为什么我在JTA EJB中的@PostConstruct方法中得到TransactionRequiredException? [英] Why am I getting a TransactionRequiredException in @PostConstruct method in JTA EJB?

查看:147
本文介绍了为什么我在JTA EJB中的@PostConstruct方法中得到TransactionRequiredException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java EE项目,我想在 @PostConstruct 方法中使用注入的JTA EntityManager 。由于 javax.persistence.TransactionRequiredException EntityManager.persist 失败。通过注入JSF托管bean的EJB实例调用它会成功。使用 @Resource UserTransaction UserTransaction.begin / commit EntityManager.getTransaction手动启动事务.begin / commit 因为它是JTA EntityManager

I have Java EE project where I want to use an injected JTA EntityManager in the @PostConstruct method. EntityManager.persist fails due to javax.persistence.TransactionRequiredException. It succeeds when called through an EJB instance injected into a JSF managed bean. Manually starting a transaction with @Resource UserTransaction and UserTransaction.begin/commit or EntityManager.getTransaction.begin/commit because it's a 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 数据源(它是基于JDBC连接池的GlassFish 4.1 JDBC资源,引用带有网络驱动程序的Derby数据库)。 https://github.com/krichter722/javaee-persist-in上提供的所有其他内容-postconstruct

I didn't know how to provide the jdbc/example1 data source (it's GlassFish 4.1 JDBC resource based on a JDBC connection pool referring to a Derby database with network driver). Everything else it available at https://github.com/krichter722/javaee-persist-in-postconstruct.

我读了持久存在于@PostConstruct:javax.persistence.TransactionRequiredException ,超出了中的简单 EntityManager.persist 调用的示例@ PostConstruct http://www.tikalk.com/java/doing-transactional-work-spring-service-using-postconstruct-method/ 指的是我没有使用的Spring。我没有发现 @PersistenceContext EntityManager @PostConstruct 中的行为不同。

I read Persisting in @PostConstruct: javax.persistence.TransactionRequiredException which exceeds the example of a simple EntityManager.persist call in @PostConstruct and http://www.tikalk.com/java/doing-transactional-work-spring-service-using-postconstruct-method/ which refers to Spring which I'm not using. I found no statement that @PersistenceContext EntityManager behaves differently in @PostConstruct.

推荐答案


该术语EJB规范中使用未指定的事务上下文来指代EJB体系结构未完全定义企业bean方法执行的事务语义的情况。

这包括以下情况:< br>
...

•使用容器管理的事务划分执行无状态会话bean的PostConstruct或PreDestroy回调方法。

The term "an unspecified transaction context" is used in the EJB specification to refer to the cases in which the EJB architecture does not fully define the transaction semantics of an enterprise bean method execution.
This includes the following cases:
...
• The execution of a PostConstruct or PreDestroy callback method of a stateless session bean with container-managed transaction demarcation.

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

An alternative solution may be to specify the javax.persistence.sql-load-script-source property in your persistence.xml file. It points at a SQL script that will preload your database. This can be a resource embedded in your application or a file URL.

这篇关于为什么我在JTA EJB中的@PostConstruct方法中得到TransactionRequiredException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆