javahibernatejdbctransactionsautocommit

Hibernate ignoring Autocommit false setting. Creating a local connecction instead


I am trying to batch saveorUpdate new entities to a mysql db via hibernate. I put a transaction.begin and a transaction.commit around a loop that does the entity generation and passes it to session.saveorUpdate. However on each pass, hibernate connects to DB, instead of commiting at the end when I call transaction.commit(). On checking logs, hibernate outputs : Connection 'local transaction' will be committed and connection will be set to auto commit mode

So far I have, 1) have tried adding disabledLocalTxn to true to connection url, but no effect, 2) have set autocommit to false property but no end effect

Hibernate properties:

<property name="hibernate.connection.url">jdbc:mysql://url:port/Dbname?disableLocalTxn=true</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <property name="hibernate.show_sql">false</property>
  <property name="hibernate.hbm2ddl.auto">validate</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.jdbc.batch_size">50</property>
  <property name="hibernate.order_inserts">true</property>
  <property name="hibernate.connection.autocommit">false</property>
  <property name="hibernate.hbm2ddl.jdbc_metadata_extraction_strategy">individually</property>
  <mapping class="Object1"/>
  <mapping class="Object2"/>

The code:

tx = session.getTransaction()
tx.begin();

for(i in elements){

session.saveOrUpdate(generateObject1(i));// - Hibernate queries here 

}
tx.commit;// - instead of all at end

Log output below, last line saying connection will be set to auto commit

Jun 11, 2019 9:26:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 11, 2019 9:26:06 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jun 11, 2019 9:26:12 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Jun 11, 2019 9:26:13 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Jun 11, 2019 9:26:27 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@2257fadf] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.

Solution

  • To end this in case anyone has somehow reached here.

    The auto commit is required as the ddl statements are set to update. SO only the ddl statements run in auto-commit true mode

    However when you do manual transaction handling via session.beginTransaction and tx.commit it runs as it would in auto-commit false mode.

    So all is well. Had some confusion due to extra queries when it would select on db for hibernate's saveOrUpdate()