使用 JTA 数据源在 JBoss AS 7 上配置 EJB:提交每个语句 [英] Configuring EJB on JBoss AS 7 using JTA datasources: every statement is commited

查看:28
本文介绍了使用 JTA 数据源在 JBoss AS 7 上配置 EJB:提交每个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!

我一直试图找到答案,但我没有找到.

I've been trying to find the answer for some time but I didn't manage.

我尝试使用 Enterprise Java Beans 配置我的应用程序并使其在 JBoss Application Server 7.1.1 下工作.我的应用程序是 Web 应用程序,它使用 servlet 并将其他类作为 EJB 注入.问题是每个语句都被提交,所以这意味着不支持事务管理.

I try to configure my application and make it work under JBoss Application Server 7.1.1, using Enterprise Java Beans. My application is Web application, it uses servlets and injects other classes as EJBs. The problem is that every statement gets commited, so that means no transaction management is supported.

在我的测试示例中,我有一个包含子项集合的实体(映射关系为 OneToMany 和属性 CascadeType.ALL).如果集合中的记录有一些问题(例如不存在的外键),则无法将其插入表并引发异常.但是,父实体被插入,所以我假设插入是在不同的单独事务中完成的.这是完全不受欢迎的行为,我尝试解决它.​​

In my test example I have an entity with a collection of children (mapped with a relationship OneToMany with a property CascadeType.ALL). If a record in a collection has some problems (e.g. non-existing foreign key), it can't be inserted into table and throws exception. However, the parent entity gets inserted, so I assume the inserts are done in different separate transactions. This is strictly undesired behavior and I try to resolve it.

技术参数:

DBMS:Oracle EE 11g

DBMS: Oracle EE 11g

AS:JBoss AS 7.1.1

AS: JBoss AS 7.1.1

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/">
   <persistence-unit name="OracleEntityManager">
      <jta-data-source>java:jboss/CmaDevDS</jta-data-source>
      <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect" />
        <property name="hibernate.hbm2ddl.auto" value="none" />
        <property name="hibernate.jdbc.batch_size" value="20" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.use_sql_comments" value="true" />
      </properties>
   </persistence-unit>
</persistence>

我的EJB:

@Stateless(name="EntityWriter")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class EntityWriter {

    @Resource
    private SessionContext context;
    /*@Resource
    UserTransaction ut;*/
    @PersistenceContext(unitName = "OracleEntityManager",type=PersistenceContextType.EXTENDED)
    EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public EntityMarker insertEntity(EntityMarker entity)throws Exception
    {
        try
        {
            entity = em.merge(entity);
            em.flush();
            return entity;
        }
        catch (Exception e)
        {
            context.setRollbackOnly();
            e.printStackTrace();
            return null;
        }
    }
}

实际上,我尝试了两种 EJB 方法:容器管理事务和 Bean 管理事务,但都没有像我预期的那样工作.

Actually I tried both EJB approaches: Container Management Transaction and Bean Management Transaction and neither works as I expect.

当我将 bean 注入 servlet 时,我是这样做的:

When I inject the bean into servlets I do it like this:

    @EJB(name = "EntityWriter")
    private EntityWriter entityWriter;

现在我认为 bean 很好,可能persistence.xml 中缺少某些东西.

Now I think the bean is fine, probably something is missing in persistence.xml.

如果有任何想法,我们将不胜感激.提前致谢!

Would be grateful to any ideas. Thanks in advance!

推荐答案

在我通过 JBoss 管理控制台编辑数据源配置并设置使用 JTA"复选框后,一切正常.默认情况下未选中.

Everything works correct after I edited Datasource configuration via JBoss Administration console and set a checkbox "Use JTA". It was unchecked by default.

你说得对,克里斯.谢谢!

You're right, Chris. Thanks!

这篇关于使用 JTA 数据源在 JBoss AS 7 上配置 EJB:提交每个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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