在这个春季文档 http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/transaction.html 。它谈到了物理和逻辑事务。我写了一个非常简单的程序来明白它
TransactionObject.java
class TransactionObject {
private static Connection conn;
TransactionObject() {
try {
conn = DriverManager.getConnection("DB_URL", "USER", "PASS");
} catch (SQLException e) {}
}
void rollBack() {
try {
conn.rollback();
} catch (SQLException e) { }
}
void Commit() {
try {
conn.commit();
} catch (SQLException e) { }
}
}
ClassMethodUnderTransactionObject.java
public class ClassMethodUnderTransactionObject {
// start fresh TransactionObject
TransactionObject logical= new TransactionObject();
M2(); // call M2 use same logical object
M3(); // call M3 use same logical object
// For M2 and M3 it is logical transaction because both using same transaction object
if(error){
logical.rollBack();
} else {
logical.Commit();
}
// create another instance of TransactionObject
TransactionObject physical=new TransactionObject();
M4(); // call M4 use different TransactionObject object
if(error){
physical.rollBack();
} else {
physical.Commit();
}
所以 M4 相对于 M2 和 M3 使用物理事务,因为 M4 使用不同的 trasnactionObject
那是我们应该理解的方式吗?还有更多要理解的内容。请帮助我理解它。