Search code examples
javahibernateweblogic

Weblogic and hibernate configuration


I need to migrate a project that uses hibernate from Jboss to weblogic. Currently I'm using this configuration:

persistence.xml

<persistence-unit name="pagosHibernate" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>...</class>
    <properties>
        <property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/>
    </properties>
</persistence-unit>

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">MyDS</property>
        <property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
    </session-factory>
</hibernate-configuration>

And the code is:

EntityManager em = this.factory.createEntityManager();
EntityTransaction entityTransaction = em.getTransaction();
entityTransaction.begin();
...
em.persist(device);
entityTransaction.commit();

But I'm getting:

java.sql.SQLException: Cannot call commit when using distributed transactions

Thanks


Solution

  • The problem was caused by the XA datasource as R Sawant said. Nevertheless the solution was creating a non-XA datasource and disabling global transactions (keeping global transactions activated persisted the problem).

    I would like to know how to configure this project to work on XA databases but that would be another question.