【发布时间】:2014-07-18 08:12:47
【问题描述】:
这是我的课:
@Stateless
public class MyClass {
@Inject
UserTransaction tx;
@PersistenceContext
EntityManager em;
public void myMethod() throws Exception {
try {
// ...
tx.begin();
updateTable1();
updateTable2();
tx.commit();
} catch(Exception ex) {
tx.rollback();
throw ex;
}
}
private void updateTable1() {
Query query = em.createNamedQuery(Table1.updateQuery);
query.executeUpdate();
}
private void updateTable2() {
Query query = em.createNamedQuery(Table2.updateQuery);
query.executeUpdate();
}
}
JTA 数据源在 persistence.xml 中定义。
这是我对这个类的理解:tx.begin() 从连接池中获取一个连接。此连接将由实体管理器在 updateTable1() 和 updateTable2() 中使用。 tx.commit() 在将更改提交到数据库后将连接返回到连接池。我说的对吗?
我是否可以在 myMethod() 中获取事务中使用的 JDBC 连接?
【问题讨论】:
标签: java jpa transactions entitymanager jta