Zha*_* Yi 4 jboss jpa datasource jta wildfly
在 JBoss/WildFly 中,在配置数据源时,有一个 JTA 选项,默认是禁用的:
<datasource jta="false" jndi-name="java:/wt/testds" pool-name="testds" enabled="true" use-ccm="false">
...
</datasource>
Run Code Online (Sandbox Code Playgroud)
现在我想使用 JTA 事务类型将此数据源与 JPA 相关联:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>java:/wt/testds</jta-data-source>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
我还需要在数据源上启用 JTA 吗?
是的,当然,如果您想进行 jta 事务,您需要在数据源上启用 JTA!
您的 XML/JBoss/Wildfly 配置文件将如下所示:
<datasource jta="true" ...
Run Code Online (Sandbox Code Playgroud)
在我们的 webapp 持久化单元中,数据源如下所示:
<jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>
Run Code Online (Sandbox Code Playgroud)
这transaction-type="JTA"不是必需的,至少在我的设置(Wildfly 8.1)中不是。
在您的 Java 代码中,您可以像这样使用事务:
@TransactionManagement(TransactionManagementType.CONTAINER) // class level
public class ...
...
@PersistenceContext(unitName = "CoreJPA")
EntityManager em;
@Resource
private EJBContext ejbContext;
...
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // method level
public void doSomething(...)
Run Code Online (Sandbox Code Playgroud)
如果你需要回滚,你可以这样做:
try {
...
} catch (Throwable t) {
log.error("Exception in create work order: " + t.getMessage());
ejbContext.setRollbackOnly();
throw t;
}
Run Code Online (Sandbox Code Playgroud)
可以使用 Google 找到很多关于此的资源。
| 归档时间: |
|
| 查看次数: |
16654 次 |
| 最近记录: |