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

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

问题描述

每个人!

我一直试图找到答案一段时间,但我没有管理。

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

我尝试配置我的应用程序,并使用Enterprise Java Bean在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天全站免登陆