尝试将数据插入数据库时​​发生TransactionRequiredException

时间:2017-09-19 14:45:12

标签: java jpa transactions persistence jta

我正在使用JPA和JTA

保存bean的存储库方法如下:

    @Override
    public UserInfo insert(UserInfo userInfo) throws Exception {

        UserInfo user = entityManager.find(UserInfo.class, userInfo.getName());

        if (user != null) {
            throw new IllegalArgumentException("bla bla bla");
        }


            entityManager.persist(userInfo);
            entityManager.flush();

        return userInfo;
    }

然后是persistence.xml文件:

<persistence-unit name="perunit" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/dta</jta-data-source>
        <class>com.yyyy.model.UserInfo</class>
        ....

每次我尝试保存bean时,flush()方法都会抛出下一个异常

javax.persistence.TransactionRequiredException: 
Exception Description: No externally managed transaction is currently active for this thread
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.throwCheckTransactionFailedException(JTATransactionWrapper.java:86)
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.checkForTransaction(JTATransactionWrapper.java:46)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:1776)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:780)

感谢任何建议,谢谢

0 个答案:

没有答案