当autocommit为ON时,“set transaction”是否会启动事务?

时间:2015-12-10 20:14:28

标签: sql oracle transactions

我正在使用Oracle和AutoCommit ON模式(使用JDBC的Java应用程序)。

当我将多个DML语句作为单个事务执行时,我认为我可以这样做:

-Threaded

然而,似乎每当我最终执行func addSkipBackupAttributeToItemAtURL(URL: NSURL) -> Bool{ let fileManager = NSFileManager.defaultManager() assert(fileManager.fileExistsAtPath(URL.absoluteString)) var error:NSError? let success:Bool = try? URL.setResourceValue(NSNumber(bool: true),forKey: NSURLIsExcludedFromBackupKey) if !success { print("Error excluding \(URL.lastPathComponent) from backup \(error)") } else { print("File at path \(URL) was succesfully updated") } return success } 时,行都会在上面的语句中给出新值。

因此,即使set transaction read write update user_tbl set name='mark' where email='m@xyz.com' update user_tbl set name='ken' where email='k@xyz.com' --if other things are successful, then: commit -- else: --rollback 在开头执行,是否有可能在AutoCommit ON模式下执行rollback语句?

1 个答案:

答案 0 :(得分:0)

这在文件中清楚地解释了: http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html
http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#setAutoCommit(boolean)

  

注意:配置Connection时,JDBC应用程序应使用相应的Connection方法,例如setAutoCommit或setTransactionIsolation。 当有可用的JDBC方法时,应用程序不应直接调用SQL命令来更改连接的配置。默认情况下,Connection对象处于自动提交模式,这意味着它在执行每个语句后自动提交更改。如果已禁用自动提交模式,则必须显式调用方法commit才能提交更改;否则,数据库更改将不会保存。

  

<强>的setAutoCommit
   void setAutoCommit(boolean autoCommit)                      抛出SQLException
  将此连接的自动提交模式设置为给定状态。 如果连接处于自动提交模式,则其所有SQL语句将作为单个事务执行并提交。否则,其SQL语句将分组为通过调用方法终止的事务提交或方法回滚。默认情况下,新连接处于自动提交模式。   语句完成时发生提交。语句完成的时间取决于SQL语句的类型:

     

对于DML语句,例如Insert,Update或Delete,以及DDL语句,语句在完成执行后立即完成。   对于Select语句,关闭关联结果集时语句完成。   对于CallableStatement对象或返回多个结果的语句,当关闭所有关联的结果集并且已检索到所有更新计数和输出参数时,语句将完成。   注意:如果在事务期间调用此方法并且更改了自动提交模式,则提交事务。如果调用setAutoCommit并且未更改自动提交模式,则调用是无操作。

以上意思是,您的代码在自动提交模式下运行,相当于:

set transaction read write;
commit; -- invoked by JDBC autocommit
update user_tbl set name='mark' where email='m@xyz.com';
commit; -- invoked by JDBC autocommit
update user_tbl set name='ken' where email='k@xyz.com';
commit; -- invoked by JDBC autocommit
--if other things are successful, then:
commit;
commit; -- invoked by JDBC autocommit
相关问题