为什么我在JTA EJB中的@PostConstruct方法中得到TransactionRequiredException?

时间:2017-03-31 18:54:38

标签: java jpa java-ee jdbc glassfish-4

我有Java EE项目,我希望在EntityManager方法中使用注入的JTA @PostConstruct。由于EntityManager.persistjavax.persistence.TransactionRequiredException失败。通过注入JSF托管bean的EJB实例调用它会成功。手动启动与@Resource UserTransactionUserTransaction.begin/commitEntityManager.getTransaction.begin/commit的交易,因为它是JTA EntityManager

EJB接口

@Local
public interface UserService extends Serializable {

    public void saveUser(AUser user);
}

@Entity
public class AUser implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;
    private String username;

    public AUser() {
    }

    public AUser(String username) {
        this.username = username;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

EJB实现:

@Stateless
public class DefaultUserService implements UserService {
    private static final long serialVersionUID = 1L;
    @PersistenceContext
    private EntityManager entityManager;

    public DefaultUserService() {
    }

    @PostConstruct
    private void init() {
        AUser user = new AUser("initUser");
        saveUser(user);
    }

    @Override
    public void saveUser(AUser user) {
        entityManager.persist(user);
    }
}

persistence.xml

<?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="richtercloud_javaee-persist-in-postconstruct-jar_jar_1.0-SNAPSHOTPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/example1</jta-data-source>
    <class>richtercloud.javaee.persist.in.postconstruct.jar.entities.AUser</class>
    <properties>
      <property name="eclipselink.target-database" value="Derby"/>
      <!-- necessary in order to avoid syntax errors -->
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
      <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
      <property name="eclipselink.target-server" value="Glassfish"/>
      <!-- avoid non-severe NullPointerException being logged in GlasFish
            <ref>https://java.net/jira/browse/GLASSFISH-21468f</ref>-->
    </properties>
  </persistence-unit>
</persistence>

我不知道如何提供jdbc/example1数据源(它的GlassFish 4.1 JDBC资源基于JDBC连接池,引用带有网络驱动程序的Derby数据库)。 https://github.com/krichter722/javaee-persist-in-postconstruct上提供的所有其他内容。

我看过Persisting in @PostConstruct: javax.persistence.TransactionRequiredException超出了EntityManager.persisthttp://www.tikalk.com/java/doing-transactional-work-spring-service-using-postconstruct-method/中简单@PostConstruct来电的示例,其中提到了我没有使用的Spring。我没有发现@PersistenceContext EntityManager@PostConstruct中表现不同的声明。

2 个答案:

答案 0 :(得分:3)

它不保证@PostConstruct和@PreDestroy是交易的一部分。所以不应该在PostConstruct方法和PreDestroy方法中进行DB操作。

答案 1 :(得分:1)

EJB 3.2规范的第8.6.2节声明:

  

EJB规范中使用术语“未指定的事务上下文”来指代EJB体系结构未完全定义企业bean方法执行的事务语义的情况。
  这包括以下情况:
  ...
   •使用容器管理的事务划分执行无状态会话Bean的PostConstruct或PreDestroy回调方法。

另一种解决方案可能是在persistence.xml文件中指定javax.persistence.sql-load-script-source属性。它指向将预加载数据库的SQL脚本。这可以是应用程序中嵌入的资源或文件URL。