jdbc autocommit(false)不起作用 [英] jdbc autocommit(false) doesnt work

查看:247
本文介绍了jdbc autocommit(false)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.sql.Connection.commit()有些东西我不理解。

我正在使用Derby(Java DB)作为数据库服务器。

I am using Derby(Java DB) as database server.

当我执行 setAutoCommit(false)时,我希望查询无法正常工作在我显式调用commit()方法之前。
,但实际上,即使我不调用commit(),它仍然会提交。
,当我在表上调用select *打印内容时,即使我没有明确提交查询,我也可以看到已添加行。

when I do a setAutoCommit(false) , I expect my query not to work before I explicitly call the commit() method. but in fact, it still commit even if I don't call commit(). when I call a select * on my table to print the content, I can see that the rows have been added even though i didn't explicitly commit the query.

有人可以给我一些解释吗?

Could someone give me some explanation please?

    con.setAutoCommit(false);
    PreparedStatement updateHair = null;
    PreparedStatement addMan = null;
    try {

         String updateString =
                    "update PERSONNE " +
                    "set haircolor = 'RED' where haircolor = 'SHAVE'";

         String updateStatement =
                    "insert into personne values " +
                    "(3,'MICHEL','SHAVE')";

         addMan = con.prepareStatement(updateStatement);
         addMan.executeUpdate();

         updateHair = con.prepareStatement(updateString);
         updateHair.executeUpdate();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


推荐答案

自动提交表示每个单个SQL语句被视为事务,并在执行后立即自动提交。缺省情况是,SQL语句在完成时(而不是在执行时)提交。检索到其所有结果集和更新计数后,该语句即完成。但是,在几乎所有情况下,语句都是在执行后立即完成并因此提交的。

Auto-commit means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. The default is for a SQL statement to be committed when it is completed, not when it is executed. A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.

允许将两个或多个语句组合为一个语句的方式事务是禁用自动提交模式。

The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode.

con.setAutoCommit(false);

禁用自动提交模式后,除非您显式调用方法commit,否则不会提交任何SQL语句。 。在上一次调用方法commit之后执行的所有语句都包含在当前事务中,并作为一个单元一起提交。

When the auto-commit mode is disabled, no SQL statements are committed until you call the method commit explicitly. All statements executed after the previous call to the method commit are included in the current transaction and committed together as a unit.

-EDIT_1

由于您在不调用 rollback()的情况下关闭连接而可能提交了更新。

Updates may be committed because you're closing your Connection without calling rollback().

如果在没有显式提交或回滚的情况下关闭了连接,则行为取决于数据库。

If a Connection is closed without an explicit commit or a rollback the behaviour depends on database.


强烈建议应用程序在调用close方法之前显式提交或
回滚活动事务。如果
调用close方法并且有一个活跃的事务,则
的结果是实现定义的。

It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined.

Connection.close()

这篇关于jdbc autocommit(false)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆