环境:
我有一个带有持久性单元(OracleDS)的war项目,它可以使用另一个持久性单元(MySqlDS)访问ejb.jar。
服务器已将两个数据源都配置为Non-XA。
默认事务管理是类型Container,但如果我在类级别使用Transaction ManagementType.BEAN,它似乎可以工作。
为什么这样?什么是容器类型的差异?
@Stateless类中的代码
public void test() {
Espacio espacioBase = espacioMgr.findByCodEspai(1); //EJB with 1st datasource
Servei servei = ServeiMgr.findById(1); //EJB with 2nd datasource ERROR
}
Espacio.java new.war
@Entity
@Table(name = "V_ESPACIOS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = Espacio.findByCodEspai, query = "SELECT e FROM Espacio e WHERE e.codespai = :codespai")})
public class Espacio implements Serializable {
private static final long serialVersionUID = 1L;
public static final String findByTipus = "Espacio.findByTipus";
public static final String findByCodEspaiPare = "Espacio.findByCodEspaiPare";
public static final String findByNom = "Espacio.findByNom";
public static final String findAll = "Espacio.findAll";
public static final String findByCodEspai = "Espacio.findByCodEspai";
public static final String findChildrenByCodEspai = "Espacio.findChildrenByCodEspai";
...
Servei.java ejb.jar
@Entity
@Table(name = "a_servei")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = Servei.findAll, query = "SELECT s FROM Servei s")})
public class Servei implements Serializable, Comparable<Servei> {
private static final long serialVersionUID = -1979740551238968079L;
static final String PREFIX = "pia.entity.Servei.";
public static final String findAll = PREFIX + "findAll";
...
ServeiManager
@Stateless
public class ServeiManager {
protected Logger logger = Logger.getLogger(getClass().getName());
@PersistenceContext(unitName = "onePU")
protected EntityManager em;
public List<Servei> all() {
return this.em.createNamedQuery(Servei.findAll, Servei.class).
getResultList();
}
...
EspacioManager
@Stateless
public class EspacioManager {
protected Logger logger = Logger.getLogger(getClass().getName());
@PersistenceContext(unitName = "twoPU")
protected EntityManager em;
public Espacio findByCodEspai(int codEspai) {
return this.em.createNamedQuery(Espacio.findByCodEspai, Espacio.class).
setParameter("codespai", codEspai).
getSingleResult();
}
...
persistence.xml new.war
<?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="twoPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jboss/datasources/OracleDS</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
persistence.xml ejb.jar
<?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="onePU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
错误
09:24:46,961 WARN [com.arjuna.ats.arjuna] (default task-18) ARJUNA012140: Adding multiple last resources is disallowed. Trying to add LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@425d04d5[connectionListener=1949f99c connectionManager=76ebb8c8 warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff7f000101:2b71c2d2:5a701dd6:13d, node_name=1, branch_uid=0:ffff7f000101:2b71c2d2:5a701dd6:143, subordinatenodename=null, eis_name=java:jboss/datasources/MySqlDS > productName=MySQL productVersion=5.5.44-MariaDB-log jndiName=java:jboss/datasources/MySqlDS])), but already have LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@47bca654[connectionListener=2b07cb93 connectionManager=214b627a warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff7f000101:2b71c2d2:5a701dd6:13d, node_name=1, branch_uid=0:ffff7f000101:2b71c2d2:5a701dd6:141, subordinatenodename=null, eis_name=java:jboss/datasources/OracleDS > productName=Oracle productVersion=Oracle Database 11g Release 11.2.0.4.0 - 64bit Production
With the Automatic Storage Management option jndiName=java:jboss/datasources/OracleDS]))
09:24:46,966 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-18) SQL Error: 0, SQLState: null
09:24:46,967 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-18) javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@1949f99c[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@33011f78 connection handles=0 lastReturned=1517300686965 lastValidated=1517297117411 lastCheckedOut=1517300686959 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@35406018 mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool@2e374d12[pool=MySqlDS] xaResource=LocalXAResourceImpl@425d04d5[connectionListener=1949f99c connectionManager=76ebb8c8 warned=false currentXid=null productName=MySQL productVersion=5.5.44-MariaDB-log jndiName=java:jboss/datasources/MySqlDS] txSync=null]
09:24:46,970 INFO [org.hibernate.event.internal.DefaultLoadEventListener] (default task-18) HHH000327: Error performing load command : org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
09:24:47,057 ERROR [org.jboss.as.ejb3.invocation] (default task-18) WFLYEJB0034: EJB Invocation failed on component ServeiManager for method public es.imas.ada.business.gestio.entity.Servei es.imas.ada.business.gestio.boundary.ServeiManager.findById(int): javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
... more
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
... more
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
... more
Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@1949f99c[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@33011f78 connection handles=0 lastReturned=1517300686965 lastValidated=1517297117411 lastCheckedOut=1517300686959 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@35406018 mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool@2e374d12[pool=MySqlDS] xaResource=LocalXAResourceImpl@425d04d5[connectionListener=1949f99c connectionManager=76ebb8c8 warned=false currentXid=null productName=MySQL productVersion=5.5.44-MariaDB-log jndiName=java:jboss/datasources/MySqlDS] txSync=null]
... 240 more
Caused by: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@1949f99c[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@33011f78 connection handles=0 lastReturned=1517300686965 lastValidated=1517297117411 lastCheckedOut=1517300686959 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@35406018 mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool@2e374d12[pool=MySqlDS] xaResource=LocalXAResourceImpl@425d04d5[connectionListener=1949f99c connectionManager=76ebb8c8 warned=false currentXid=null productName=MySQL productVersion=5.5.44-MariaDB-log jndiName=java:jboss/datasources/MySqlDS] txSync=null]
... 244 more
Caused by: javax.resource.ResourceException: IJ000461: Could not enlist in transaction on entering meta-aware object
... 246 more
Caused by: javax.transaction.SystemException: IJ000356: Failed to enlist: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 0:ffff7f000101:2b71c2d2:5a701dd6:13d status: ActionStatus.ABORT_ONLY >
... 247 more