第一次交易失败,IJ000457,IJ000461和IJ000356,但之后有效

时间:2015-06-14 21:46:57

标签: jpa ejb wildfly jta

我正在尝试使用JPA在Wildly服务器上运行JSF应用程序,我有以下代码,在第一次尝试添加员工时有一个例外,但是当尝试另一次时,它工作正常,我没有找到原因,请帮忙。

会话bean:

var size = Marshal.SizeOf(typeof(T));
var subBuffer = new byte[size];
Array.Copy(Buff, Peek, subBuffer, 0, size);
var handle = GCHandle.Alloc(subBuffer, GCHandleType.Pinned);
var ptr = handle.ToIntPtr();
var val = (T)Marshal.PtrToStructure(ptr, typeof(T));
ptr.Free();
Peek += size;
Peek = ( Peek + ( Align - 1 ) ) & ~( Align - 1 );
return val;

的persistence.xml

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.Transactional;

import model.Employee;

@Stateless
@Transactional(rollbackOn=Exception.class)
public class EmployeeBean {
    @PersistenceContext(unitName="employee")
       private EntityManager em;

       public void create(Employee... gs) {
           if(em==null)
           {
               System.out.println("entitymanager is null");
               return;

           }


           try {
            for(Employee g : gs) {
                   em.persist(g);
               }
        } catch (Exception e) {
            System.out.println("Error");
            e.printStackTrace();
        }
       }

}

standalone.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="employee" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/employee</jta-data-source>
        <class>model.Emp_Proj</class>
        <class>model.Emp_ProjPK</class>
        <class>model.Project</class>
        <class>model.Employee</class>
    </persistence-unit>
</persistence>

错误:

<datasources>
                <datasource jta="true" jndi-name="java:jboss/datasources/employee" pool-name="MysqlDS2" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/employee</connection-url>
                    <driver>mysqld</driver>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>20</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>root</user-name>
                        <password>123456</password>
                    </security>
                    <timeout>
                        <idle-timeout-minutes>1000</idle-timeout-minutes>
                    </timeout>
                </datasource>
                <drivers>
                    <driver name="mysqld" module="com.mysql">
                        <driver-class>com.mysql.jdbc.Driver</driver-class>
                    </driver>
                </drivers>
            </datasources>

3 个答案:

答案 0 :(得分:7)

这是一个奇怪的错误,发生在我身上,请尝试禁用数据源中的jta,例如 jta="false" 或者将WildFly升级到8.2.0最终版本。

答案 1 :(得分:1)

我遇到了非常相似的问题java.sql.SQLException: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected(),然后我尝试重复该操作,但操作成功。总是第一个操作会产生此错误。在这里,我使用的是wildfly-9.0.0.Final,并用datasource jta="false"更改了standalone.xml之后,它就起作用了。

做了一些测试,到目前为止很好。谢谢。

答案 2 :(得分:0)

同样的问题,原因是在bean类级别缺少@TransactionManagement(TransactionManagementType.BEAN)注释。添加后,情况已解决。