休眠时忽略自动提交错误设置。而是创建本地连接

时间:2019-06-11 16:20:12

标签: java hibernate jdbc transactions autocommit

我正在尝试通过休眠将saveorUpdate新实体批处理到mysql数据库中。我将transaction.begin和transaction.commit放在执行实体生成并将其传递给session.saveorUpdate的循环周围。但是,每次通过时,hibernate都连接到DB,而不是在我调用transaction.commit()时在最后提交。在检查日志时,休眠输出:将提交连接“本地事务”,并将连接设置为自动提交模式

到目前为止,我已经知道:1)尝试将disabledLocalTxn添加到连接网址的true中,但是没有效果, 2)已将autocommit设置为false属性,但没有结束效果

休眠属性:

<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"/>

代码:

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

for(i in elements){

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

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

下面的日志输出,最后一行表示连接将被设置为自动提交

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.

1 个答案:

答案 0 :(得分:0)

如果有人以某种方式到达此处,结束此操作。

由于ddl语句设置为更新,因此需要自动提交。所以只有ddl语句在自动提交真实模式下运行

但是,当您通过session.beginTransaction和tx.commit进行手动事务处理时,它会像在自动提交错误模式下一样运行。

一切都很好。由于它会在db上选择hibernate的saveOrUpdate()

时,由于额外的查询而有些混乱