I am using datasource to connect to Oracle database and insert data.
Below is my hibernate config entry:
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">jdbc/LOCAL_ORACLE</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
</session-factory>
</hibernate-configuration>
Below is the code snippet which saves the data.
final Configuration cfg = new Configuration().addResource("SampleTable.hbm.xml").configure();
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
txn = session.beginTransaction();
session.save(cs90spTransDetails);
txn.commit();
The issue I am facing is when txn.commit() is called hibernate throws the below error:
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116)
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:178)
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R ... 13 more
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R Caused by: java.sql.SQLException: DSRA9350E: Operation Connection.commit is not allowed during a global transaction.
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.commit(WSJdbcConnection.java:1064)
[4/24/12 19:25:13:675 IST] 0000001a SystemErr R at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112)
NOTE: The above code works fine if I use hibernate.connection.driver_class and hibernate.connection.url to connect to database. Data gets stored in the database without any issues.
I am sure I am doing something wrong, which I am not able to figure out. Can someone tell me what I am missing.
Thank You.
Jay Chandran.
As far as I can see datasource obtained from the application server is configured to participate in container-managed transactions.
Therefore you need to configure Hibernate to use these transactions (see 3.9.1. Transaction strategy configuration and 3.9.3. Current Session context management with JTA), and don’t need to manage transactions manually.