gpt4 book ai didi

java - JTA EntityManager 不能在存储过程调用中使用 getTransaction()

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:24 26 4
gpt4 key购买 nike

我想通过调用存储过程在 ejb 方法中执行异步事务操作。当我调用方法时,出现以下错误:

java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

bean

@Stateless
public class FileSearchDAO {
private static Logger logger = LoggerFactory.getLogger(FileSearchDAO.class);

@PersistenceContext(unitName = "FileSearchPU")
private EntityManager entityManager;

@Asynchronous
public Future<String> saveFile(String fileNo, List<String> runningFiles) {
try {
entityManager.getTransaction().begin();
entityManager.createNativeQuery(
" BEGIN prc_save_file (:fileNo); END;")
.setParameter("fileNo", fileNo).executeUpdate();
entityManager.getTransaction().commit();
runningFiles.remove(fileNo);
return new AsyncResult<>(fileNo);
} catch (Exception ex) {
ex.printStackTrace();
return new AsyncResult<>(ex.getMessage());
}
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="FileSearchPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/FileSearchDS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.transaction.jta.platform"
value="${hibernate.transaction.jta.platform}"/>
</properties>
</persistence-unit>
</persistence>

我没有任何实体类。我只想调用更新某些表的存储过程。

最佳答案

JTA 中,托管数据源容器以分布式方式处理事务,例如,还处理应用程序外部的并发。

EntityManager 的事务无法使用,因为它是本地事务,因此无法在您的应用程序外部处理。另请阅读 this post获取更多信息。

如果你需要交易,你应该使用UserTransaction

@Resource
UserTransaction utx;

要使用它,请注释您的 bean

@TransactionManagement(TransactionManagementType.BEAN)

并使用类似的交易

utx.begin();
...
utx.commit(); // utx.rollback();

关于java - JTA EntityManager 不能在存储过程调用中使用 getTransaction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com