如何在JDBC中启动事务? [英] How to start a transaction in JDBC?
本文介绍了如何在JDBC中启动事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Connection.setTransactionIsolation(int)
警告:
注意:如果调用此方法在事务期间,结果是实现定义的。
Note: If this method is called during a transaction, the result is implementation-defined.
这提出了一个问题:如何在JDBC中开始事务?很清楚如何结束一个交易,但不是如何开始它。
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.
如果一个连接
在一个交易中开始,我们怎么样?应该在事务之外调用 Connection.setTransactionIsolation(int)
以避免特定于实现的行为?
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?
推荐答案
回答我自己的问题:
- JDBC连接从启用自动提交模式开始,其中每个SQL语句隐式地用事务划分。
- 希望每个事务执行多个语句的用户必须转为 auto-commit off 。
- 更改自动提交模式会触发提交当前交易(如果有一个交易)。
- < a href =http://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#setTransactionIsolation-int- =noreferrer> Connection.setTransactionIsolation()如果启用了自动提交,则可以随时调用。
- 如果禁用自动提交, Connection.setTransactionIsolation()只能在事务之前或之后调用。在交易过程中调用它会导致未定义的行为。
- JDBC connections start out with auto-commit mode enabled, where each SQL statement is implicitly demarcated with a transaction.
- 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.
- 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.
来源:
- Javadoc
- JDBC Tutorial
这篇关于如何在JDBC中启动事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文