2

I am trying to save an object on Hibernate database but I am receiving following exception. It seems like session is open and connected but the code is unable to commit.

    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.

My code

Service

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

Repository

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();
        }

}

Output in Console

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)

Configuration

<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" />
3
  • 1
    I think the problem is not in that piece of code but outside of it. Something is wrong with your transaction manager. Commented Jul 19, 2015 at 5:52
  • @LuiggiMendoza I am org.springframework.orm.hibernate4.HibernateTransactionManager which part of the code should be included in the question? Commented Jul 19, 2015 at 6:20
  • How are you getting the session factory object.? Can you paste that piece of code here? Commented Jul 19, 2015 at 7:11

1 Answer 1

4

I don't think you need to close and flush session in this case because your session is opened automatically by the thread initiated by the request and gets closed automatically when request is finished. So in that request you can do any number of transactions but those transactions only gets committed at the end of the request here you are closing a session before transaction is committed so when at the end of request when the commit the session was null because you already closed it earlier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.