postgresql 什么是:“启用 autoCommit 时无法提交”错误是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6915481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:11:25  来源:igfitidea点击:

What does: "Cannot commit when autoCommit is enabled” error mean?

databasepostgresql

提问by Larry

In my program, I've got several threads in pool that each try to write to the DB. The number of threads created is dynamic. When the number of threads created is only one, all works fine. However, when there are multi-thread executing, I get the error:

在我的程序中,我在池中有几个线程,每个线程都尝试写入数据库。创建的线程数是动态的。当创建的线程数只有一个时,一切正常。但是,当有多线程执行时,我收到错误:

org.apache.ddlutils.DatabaseOperationException: org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.

org.apache.ddlutils.DatabaseOperationException: org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.

I'm guessing, perhaps since each thread executes in parallel, two threads are trying to write at the same time and giving this error.

我猜,也许因为每个线程并行执行,两个线程试图同时写入并给出此错误。

Do you think this is the case, if not, what could be causing this error?

您认为是这种情况吗,如果不是,可能导致此错误的原因是什么?

Otherwise, if what I said is the problem, what I can do to fix it?

否则,如果我说的是问题,我能做些什么来解决它?

回答by Femi

In your jdbc code, you should turn off autocommit as soon as you fetch the connection. Something like this:

在您的 jdbc 代码中,您应该在获取连接后立即关闭自动提交。像这样的东西:

DataSource datasource = getDatasource(); // fetch your datasource somehow
Connection c = null;
try{
  c = datasource.getConnection();
  c.setAutoCommit(false);