Connection.setTransactionIsolation(int)
warns:
Connection.setTransactionIsolation(int)警告:
Note: If this method is called during a transaction, the result is implementation-defined.
注意:如果在事务期间调用此方法,则结果是实现定义的。
This bring up the question: how do you begin a transaction in JDBC? It's clear how to end a transaction, but not how to begin it.
这提出了一个问题:如何在JDBC中开始事务?很清楚如何结束交易,但不知道如何开始交易。
If a Connection
starts inside in a transaction, how are we supposed to invoke Connection.setTransactionIsolation(int)
outside of a transaction to avoid implementation-specific behavior?
如果Connection在事务内部启动,我们应该如何调用事务外的Connection.setTransactionIsolation(int)以避免特定于实现的行为?
7 个解决方案
#1
41
Answering my own question:
回答我自己的问题:
- JDBC connections start out with auto-commit mode enabled, where each SQL statement is implicitly demarcated with a transaction.
- JDBC连接从启用自动提交模式开始,其中每个SQL语句都使用事务隐式划分。
- Users who wish to execute multiple statements per transaction must turn auto-commit off.
- 希望每个事务执行多个语句的用户必须关闭自动提交。
- Changing the auto-commit mode triggers a commit of the current transaction (if one is active).
- 更改自动提交模式会触发当前事务的提交(如果一个处于活动状态)。
- Connection.setTransactionIsolation() may be invoked anytime if auto-commit is enabled.
- 如果启用了自动提交,则可以随时调用Connection.setTransactionIsolation()。
- If auto-commit is disabled, Connection.setTransactionIsolation() may only be invoked before or after a transaction. Invoking it in the middle of a transaction leads to undefined behavior.
- 如果禁用自动提交,则只能在事务之前或之后调用Connection.setTransactionIsolation()。在事务中调用它会导致未定义的行为。
Sources:
资料来源:
- Javadoc
- 的Javadoc
- JDBC Tutorial
- JDBC教程
#2
21
JDBC implicitly demarcates each query/update you perform on the connection with a transaction. You can customize this behavior by calling setAutoCommit(false) to turn off the auto-commit mode and call the commit()/rollback() to indicate the end of a transaction. Pesudo code
JDBC隐式地划分您在与事务的连接上执行的每个查询/更新。您可以通过调用setAutoCommit(false)来关闭自动提交模式并调用commit()/ rollback()来指示事务的结束来自定义此行为。 Pesudo代码
try
{
con.setAutoCommit(false);
//1 or more queries or updates
con.commit();
}
catch(Exception e)
{
con.rollback();
}
finally
{
con.close();
}
Now, there is a type in the method you have shown. It should be setTransactionIsolation(int level) and is not the api for transaction demarcation. It manages how/when the changes made by one operation become visible to other concurrent operations, the "I" in ACID (http://en.wikipedia.org/wiki/Isolation_(database_systems))
现在,您显示的方法中有一种类型。它应该是setTransactionIsolation(int level)并且不是事务划分的api。它管理一个操作所做的更改如何/何时对其他并发操作可见,ACID中的“I”(http://en.wikipedia.org/wiki/Isolation_(database_systems))
#3
17
I suggest you read this you'll see
我建议你读这个你会看到的
Therefore, the first call of setAutoCommit(false) and each call of commit() implicitly mark the start of a transaction. Transactions can be undone before they are committed by calling
因此,第一次调用setAutoCommit(false)和每次调用commit()都会隐式标记事务的开始。在通过调用提交事务之前,可以撤消事务
Edit:
编辑:
Check the official documentation on JDBC Transactions
查看有关JDBC事务的官方文档
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, 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.)
创建连接时,它处于自动提交模式。这意味着每个单独的SQL语句都被视为一个事务,并在执行后立即自动提交。 (更确切地说,默认情况下,SQL语句在完成时提交,而不是在执行时提交。语句在检索到所有结果集和更新计数时完成。几乎在所有情况下, ,声明在执行后立即完成并因此被提交。)
The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. This is demonstrated in the following code, where con is an active connection:
允许将两个或多个语句分组到事务中的方法是禁用自动提交模式。这在以下代码中进行了演示,其中con是活动连接:
con.setAutoCommit(false);
con.setAutoCommit(假);
Source: JDBC Transactions
来源:JDBC交易
#4
9
Actually, this page from the JDBC tutorial would be a better read.
You would get your connection, set your isolation level and then do your updates amd stuff and then either commit or rollback.
实际上,JDBC教程中的这个页面将是一个更好的阅读。你会得到你的连接,设置你的隔离级别,然后做你的更新和东西,然后提交或回滚。
#5
2
Maybe this will answer your question: You can only have one transaction per connection. If autocommit is on (default), every select, update, delete will automatically start and commit (or rollback) a transaction. If you set autocommit off, you start a "new" transaction (means commit or rollback won't happen automatically). After some statements, you can call commit or rollback, which finishes current transaction and automatically starts a new one. You cannot have two transactions actively open on one JDBC connection on pure JDBC.
也许这会回答你的问题:每个连接只能有一个事务。如果启用了自动提交(默认),则每个选择,更新,删除都将自动启动并提交(或回滚)事务。如果将autocommit设置为off,则启动“新”事务(意味着不会自动执行提交或回滚)。在一些语句之后,您可以调用commit或rollback,它完成当前事务并自动启动一个新事务。您不能在纯JDBC上的一个JDBC连接上主动打开两个事务。
#6
2
Startingly, you can manually run a transaction, if you wish to leave your connection in "setAutoCommit(true)" mode but still want a transaction:
首先,如果您希望以“setAutoCommit(true)”模式保持连接但仍想要事务,则可以手动运行事务:
try (Statement statement = conn.createStatement()) {
statement.execute("BEGIN");
try {
// use statement ...
statement.execute("COMMIT");
}
catch (SQLException failure) {
statement.execute("ROLLBACK");
}
}
#7
0
You can use these methods for transaction:
您可以将这些方法用于事务:
- you must create the connection object like
con
- 你必须像con一样创建连接对象
con.setAutoCommit(false);
- con.setAutoCommit(假);
- your queries
- 你的疑问
- if all is true
con.commit();
- 如果一切都是真的con.commit();
- else
con.rollback();
- else con.rollback();
#1
41
Answering my own question:
回答我自己的问题:
- JDBC connections start out with auto-commit mode enabled, where each SQL statement is implicitly demarcated with a transaction.
- JDBC连接从启用自动提交模式开始,其中每个SQL语句都使用事务隐式划分。
- Users who wish to execute multiple statements per transaction must turn auto-commit off.
- 希望每个事务执行多个语句的用户必须关闭自动提交。
- Changing the auto-commit mode triggers a commit of the current transaction (if one is active).
- 更改自动提交模式会触发当前事务的提交(如果一个处于活动状态)。
- Connection.setTransactionIsolation() may be invoked anytime if auto-commit is enabled.
- 如果启用了自动提交,则可以随时调用Connection.setTransactionIsolation()。
- If auto-commit is disabled, Connection.setTransactionIsolation() may only be invoked before or after a transaction. Invoking it in the middle of a transaction leads to undefined behavior.
- 如果禁用自动提交,则只能在事务之前或之后调用Connection.setTransactionIsolation()。在事务中调用它会导致未定义的行为。
Sources:
资料来源:
- Javadoc
- 的Javadoc
- JDBC Tutorial
- JDBC教程
#2
21
JDBC implicitly demarcates each query/update you perform on the connection with a transaction. You can customize this behavior by calling setAutoCommit(false) to turn off the auto-commit mode and call the commit()/rollback() to indicate the end of a transaction. Pesudo code
JDBC隐式地划分您在与事务的连接上执行的每个查询/更新。您可以通过调用setAutoCommit(false)来关闭自动提交模式并调用commit()/ rollback()来指示事务的结束来自定义此行为。 Pesudo代码
try
{
con.setAutoCommit(false);
//1 or more queries or updates
con.commit();
}
catch(Exception e)
{
con.rollback();
}
finally
{
con.close();
}
Now, there is a type in the method you have shown. It should be setTransactionIsolation(int level) and is not the api for transaction demarcation. It manages how/when the changes made by one operation become visible to other concurrent operations, the "I" in ACID (http://en.wikipedia.org/wiki/Isolation_(database_systems))
现在,您显示的方法中有一种类型。它应该是setTransactionIsolation(int level)并且不是事务划分的api。它管理一个操作所做的更改如何/何时对其他并发操作可见,ACID中的“I”(http://en.wikipedia.org/wiki/Isolation_(database_systems))
#3
17
I suggest you read this you'll see
我建议你读这个你会看到的
Therefore, the first call of setAutoCommit(false) and each call of commit() implicitly mark the start of a transaction. Transactions can be undone before they are committed by calling
因此,第一次调用setAutoCommit(false)和每次调用commit()都会隐式标记事务的开始。在通过调用提交事务之前,可以撤消事务
Edit:
编辑:
Check the official documentation on JDBC Transactions
查看有关JDBC事务的官方文档
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, 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.)
创建连接时,它处于自动提交模式。这意味着每个单独的SQL语句都被视为一个事务,并在执行后立即自动提交。 (更确切地说,默认情况下,SQL语句在完成时提交,而不是在执行时提交。语句在检索到所有结果集和更新计数时完成。几乎在所有情况下, ,声明在执行后立即完成并因此被提交。)
The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. This is demonstrated in the following code, where con is an active connection:
允许将两个或多个语句分组到事务中的方法是禁用自动提交模式。这在以下代码中进行了演示,其中con是活动连接:
con.setAutoCommit(false);
con.setAutoCommit(假);
Source: JDBC Transactions
来源:JDBC交易
#4
9
Actually, this page from the JDBC tutorial would be a better read.
You would get your connection, set your isolation level and then do your updates amd stuff and then either commit or rollback.
实际上,JDBC教程中的这个页面将是一个更好的阅读。你会得到你的连接,设置你的隔离级别,然后做你的更新和东西,然后提交或回滚。
#5
2
Maybe this will answer your question: You can only have one transaction per connection. If autocommit is on (default), every select, update, delete will automatically start and commit (or rollback) a transaction. If you set autocommit off, you start a "new" transaction (means commit or rollback won't happen automatically). After some statements, you can call commit or rollback, which finishes current transaction and automatically starts a new one. You cannot have two transactions actively open on one JDBC connection on pure JDBC.
也许这会回答你的问题:每个连接只能有一个事务。如果启用了自动提交(默认),则每个选择,更新,删除都将自动启动并提交(或回滚)事务。如果将autocommit设置为off,则启动“新”事务(意味着不会自动执行提交或回滚)。在一些语句之后,您可以调用commit或rollback,它完成当前事务并自动启动一个新事务。您不能在纯JDBC上的一个JDBC连接上主动打开两个事务。
#6
2
Startingly, you can manually run a transaction, if you wish to leave your connection in "setAutoCommit(true)" mode but still want a transaction:
首先,如果您希望以“setAutoCommit(true)”模式保持连接但仍想要事务,则可以手动运行事务:
try (Statement statement = conn.createStatement()) {
statement.execute("BEGIN");
try {
// use statement ...
statement.execute("COMMIT");
}
catch (SQLException failure) {
statement.execute("ROLLBACK");
}
}
#7
0
You can use these methods for transaction:
您可以将这些方法用于事务:
- you must create the connection object like
con
- 你必须像con一样创建连接对象
con.setAutoCommit(false);
- con.setAutoCommit(假);
- your queries
- 你的疑问
- if all is true
con.commit();
- 如果一切都是真的con.commit();
- else
con.rollback();
- else con.rollback();