Hibernate 忽略自动提交错误设置。改为创建本地连接

Hibernate ignoring Autocommit false setting. Creating a local connecction instead

我正在尝试通过休眠将新实体批量保存或更新到 mysql 数据库。我在执行实体生成并将其传递给 session.saveorUpdate 的循环周围放置了 transaction.begin 和 transaction.commit。但是,在每次传递时,hibernate 都会连接到 DB,而不是在我调用 transaction.commit() 时在最后提交。在检查日志时,休眠输出:连接 'local transaction' 将被提交并且连接将被设置为自动提交模式

到目前为止,我已经,1) 尝试将 disabledLocalTxn 添加到 true 到连接 url,但没有效果, 2) 已将自动提交设置为 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.

为了防止有人不知何故到达这里而结束。

需要自动提交,因为 ddl 语句设置为更新。所以只有 auto-commit 真模式

中的 ddl 语句 运行

然而,当您通过 session.beginTransaction 和 tx.commit 进行手动事务处理时,它 运行s 就像在 auto-commit false 模式下一样。

所以一切都很好。当它 select 在 db 上为 hibernate 的 saveOrUpdate()

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