我正在为struts2,ejb和JPA(Hibernate作为提供)中的自学习开发一个小应用程序,将应用程序服务器作为Wildfly 10.我正在尝试配置JTA来管理与容器的事务。以下是我的代码
业务服务(EJB)
compile "com.squareup.retrofit2:retrofit:2.3.0"
compile "com.squareup.retrofit2:converter-gson:2.3.0"
compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
compile "io.reactivex.rxjava2:rxandroid:2.0.1"
compile "io.reactivex.rxjava2:rxjava:2.1.2"
DaoImpl(EJB)
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class BusinessService implements BusinessRemote {
@EJB
Dao bDao;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateAll(){
try {
dao.saveAll(xyz);
} catch (Exception e) {
e.printstacktrace();
}
try {
dao.updateAll();
} catch (Exception e) {
e.printstacktrace();
}
}
}
的persistence.xml
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class DaoImpl implements Dao {
@PersistenceContext(unitName="db")
private EntityManager entityManager;
public void saveAll(xyz) {
entityManager.persist(xyz);
}
public void updateAll() {
Query query = entityManager.createNamedQuery("getQuery.ById");
query1.executeUpdate();
}
}
但是当执行 <persistence-unit name="db" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/DB</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl" />
</properties>
</persistence-unit>
方法时,我得到了堆栈跟踪。
updateAll
似乎在触发更新查询时,不存在活动事务。当我尝试执行javax.persistence.TransactionRequiredException: Executing an update/delete query
方法时,虽然我没有收到任何异常,但数据库中没有任何内容被保存。如果我尝试刷新更改,则会给出以下异常。
saveAll
所以这意味着在上面的场景中,容器根本不会启动任何事务。似乎我的配置中缺少一些我无法找到的东西。
答案 0 :(得分:0)
我遗漏了persistence.xml
正确的是
<property name="javax.persistence.jdbc.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />