java.sql.SQLException:Connection为null

时间:2015-07-19 05:28:36

标签: java spring hibernate spring-security

我正在尝试在Hibernate数据库上保存一个对象,但我收到以下异常。似乎会话已打开并已连接,但代码无法提交。

    org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed
    ...
    org.hibernate.TransactionException: commit failed
    ...
    unable to commit against JDBC connection
    ...
    java.sql.SQLException: Connection is null.

我的代码

服务

@Override
@Transactional
public void registerUser(User user){
  ....
} 

存储库

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateJdbcException;
import org.springframework.stereotype.Repository;

@Repository
public class RegRepImpl implements RegRep {

@Autowired
SessionFactory sessionFactory;

@Override
public void registerUser(Member member) throws HibernateException,

    HibernateJdbcException {
        try {
            Session session = sessionFactory.getCurrentSession();
            if (session.isConnected()) {
                System.err.println("connected");
            }
            if (session.isOpen()) {
                System.err.println("is open");
            }
            session.save(member);
            session.flush();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

}

在控制台中输出

connected
is open
Hibernate: 
    /* insert com.myproject.model.Member
        */ insert 
        into
            Member
            (authority,enabled, fname, lname, password,username) 
        values
            (?, ?, ?, ?, ?, ?)
Jul 19, 2015 1:59:22 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mp] in context with path [/myproject2] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed] with root cause
java.sql.SQLException: Connection is null.
    at org.apache.commons.dbcp2.DelegatingConnection.checkOpen(DelegatingConnection.java:608)
    at org.apache.commons.dbcp2.DelegatingConnection.commit(DelegatingConnection.java:362)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:180)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)

配置

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:8889/myproject" />
    <property name="username" value="daniel" />
    <property name="password" value="daniel" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    depends-on="dataSource">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.myproject.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>


<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

1 个答案:

答案 0 :(得分:2)

Okies。在这种情况下,我认为您不需要关闭和刷新会话,因为会话由请求启动的线程自动打开,并在请求完成时自动关闭。因此,在该请求中,您可以执行任意数量的事务,但这些事务仅在请求结束时提交,您在事务提交之前关闭会话,因此在请求结束时提交会话为空因为您已经早点关闭了。