简体   繁体   English

在单个java.sql.Statement上执行多个DML

[英]Execute Multiple DML on single java.sql.Statement

How transactions will behave; 交易行为如何; If I use following syntax to execute multiple DML using single java.sql.Statement 如果我使用以下语法使用单个java.sql.Statement执行多个DML

String sqlStr = "INSERT INTO.... \n update t1 set....";

statemet.execute(sqlStr);

I am not altering default connection setting (autocommit = true). 我没有更改默认的连接设置(autocommit = true)。

I am aware this is not the right way / best practice; 我知道这不是正确的方法/最佳做法; just curious to know the transactional nature of above code That is if update fails with SQLException, will insert rollback? 只是好奇地知道上述代码的事务性质,即如果SQLException更新失败,是否将插入回滚?

With autocommit set to true, then the individual statements would very likely execute in their own individual transactions. autocommit设置为true的情况下,单个语句很可能会在其自己的单个事务中执行。 This might depends on the database, but I suspect not. 这可能取决于数据库,但我怀疑不是。

Best way to find out is to try it. 找出答案的最佳方法是尝试一下。

I tried in MySQL environment. 我在MySQL环境中尝试过。 It worked very well. 效果很好。 And I think it is the basic way to do so. 我认为这是这样做的基本方法。

Tried out, got some surprising results with MS SQL sever 2008 尝试使用MS SQL Sever 2008获得了一些令人惊讶的结果

I tried 2 update statements and as per observation they run in a single transaction; 我尝试了2条更新语句,根据观察,它们在单个事务中运行; that is if second update fails first update statement is rolled back. 也就是说,如果第二个更新失败,则第一个更新语句将回滚。

The surprising thing I noticed is : 我注意到的令人惊讶的是:

https://connect.microsoft.com/SQLServer/feedback/details/620522/invalid-object-exception-not-thrown-by-com-microsoft-sqlserver-jdbc-sqlserverdriver# https://connect.microsoft.com/SQLServer/feedback/details/620522/invalid-object-exception-not-thrown-by-com-microsoft-sqlserver-jdbc-sqlserverdriver#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用java.sql.Statement执行多行sybase语句 - Execute Multiline sybase statement with java.sql.Statement 为什么java.sql.Statement是接口而不是抽象类? - Why is java.sql.Statement an interface and not an abstract class? 为什么java.sql.Statement破坏了向后兼容性? - Why does java.sql.Statement break backward compatibility? 寻找一种方法来防止在项目中使用java.sql.Statement - Looking for a way to prevent to usage of java.sql.Statement in project 在选中的Exception上无法清除java.sql.Statement - Fail to cleanup java.sql.Statement on checked Exception java.sql.Statement或java.sql.PreparedStatement - 带参数的可滚动结果集 - java.sql.Statement or java.sql.PreparedStatement - scrollable resultset with parameters java.sql.Connection.close()对java.sql.Statement对象等的影响 - Impact of java.sql.Connection.close() on java.sql.Statement objects and the like Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.createStatement() 对空对象引用 - Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.createStatement() on a null object reference 如何使用Java执行多个SQL语句? - How to execute multiple SQL statement using Java? 使用java使用语句类的单个execute()方法执行多个hive查询 - Execute multiple hive queries using single execute() method of statement class using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM