Thomas Schmidt
Thomas Schmidt

Reputation: 1378

Hibernate, Wildfly, JNDI Config: Cannot commit during a managed transaction

I'm currently facing a problem using Hibernate and Wildfly in a Java-Web-Application:

Exception:

Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:180) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    ... 194 more
Caused by: java.sql.SQLException: You cannot commit during a managed transaction!
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:1065)
    at org.jboss.jca.adapters.jdbc.WrappedConnection.commit(WrappedConnection.java:758)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    ... 195 more

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">java:/jdbc/salome-database</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.archive.autodetection">class</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <mapping resource="hibernate-mapping.xml"/>

    </session-factory>
</hibernate-configuration>

HibernateUtil.java

public static SessionFactory getSessionFactory()
{
    if (sessionFactory == null)
    {
        // loads configuration and mappings
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // builds a session factory from the service registry
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }

    return sessionFactory;
}

EventDAO.java

public Event getById(int id)
{
    List<Event> eventList = new ArrayList<Event>();

    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();

    try
    {
        Query q = session.createQuery("from Event as event where event.id='" + id + "'");
        q.setCacheable(true);
        eventList = (List<Event>) q.list();
        session.getTransaction().commit();
    }

    catch (RuntimeException e)
    {
        throw e;
    }

    finally
    {
    }

    if (eventList.isEmpty())
    {
        return null;
    }
    else
    {
        return eventList.get(0);
    }
}

public void addEvent(Event event)
{
    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();

    try
    {
        session.save(event);
        session.getTransaction().commit();
    }

    catch (RuntimeException e)
    {
        throw e;
    }

    finally
    {
    }
}

public void deleteEvent(Event event)
{
    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();

    try
    {
        session.delete(event);
        session.getTransaction().commit();
    }

    catch (RuntimeException e)
    {
        throw e;
    }

    finally
    {
    }
}

I already did a lot of searching and found a thread where the solution was to remove all the Transaction handling, because JTA will take care of that. When I remove the transaction.commit() statement I get another exception notifying me that nested transactions aren't supported.

Edit: I created the JNDI / data source with following statement in the wildfly console:

data-source add --name=salome-datasource --jndi-name=java:/jdbc/salome-database --driver-name=mysql-connector-java-5.1.35.jar_com.mysql.jdbc.Driver_5_1 --connection-url=jdbc:mysql://127.0.0.1:3306/salomeapp --user-name=*** --password=***

Upvotes: 0

Views: 1738

Answers (1)

ChristopherS
ChristopherS

Reputation: 883

You should also remove the session.getTransaction().begin();, together with the session.getTransaction().commit();.

When you use container managed transactions, you should never start a transaction yourself. Container managed transactions are the way the go when you don't know what you are doing.

You should read a little about container managed transactions in java ee.

Upvotes: 1

Related Questions