我想在部署在Glassfish上的Java EE應用程序中的同一事務中使用2個持久性單元。EJB3 - 在事務中使用2個持久性單元(例外:本地事務已經有1個非XA資源)
的2個持久性單元在persistence.xml中所定義,如下:
<persistence-unit name="BeachWater">
<jta-data-source>jdbc/BeachWater</jta-data-source>
...
<persistence-unit name="LIMS">
<jta-data-source>jdbc/BeachWaterLIMS</jta-data-source>
...
這些持久性單元對應於JDBC資源和我在Glassfish的已定義如下連接池(包括一個這裏因爲二者都是除了名稱相同&數據庫連接信息):
JDBC Resource:
JNDI Name: jdbc/BeachWaterLIMS
Pool Name: BEACHWATER_LIMS
Connection Pool:
Name: BEACHWATER_LIMS
Datasource Classname: com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
Resource Type: javax.sql.ConnectionPoolDataSource
有3個無狀態會話bean,LimsServiceBean,AnalysisServiceBean和AnalysisDataTransformationServiceBean。
下面是相關片段從LimsServiceBean:
@PersistenceContext(unitName = "LIMS")
EntityManager em;
...
public ArrayList<Sample> getLatestLIMSData() {
Query q = em.createNamedQuery("Sample.findBySubTypeStatus");
return new ArrayList<Sample>(q.getResultList());
}
從AnalysisServiceBean:
@PersistenceContext(unitName = "BeachWater")
EntityManager em;
...
public ArrayList<AnalysisType> getAllAnalysisTypes() {
Query q = em.createNamedQuery("AnalysisType.findAll");
return new ArrayList<AnalysisType>(q.getResultList());
}
而且從AnalysisDataTransformationServiceBean:
@EJB
private AnalysisService analysisService;
@EJB
private LimsService limsService;
public void transformData() {
List<AnalysisType> analysisTypes = analysisService.getAllAnalysisTypes();
ArrayList<Sample> samples = limsService.getLatestLIMSData();
這調用limsService.getLatestLIMSData()引起以下例外:
[exec] Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
[exec] Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: java.lang.IllegalStateException: Local transaction already has 1 non-XA Resource: cannot add more resources.
經徵詢本頁面,http://msdn.microsoft.com/en-us/library/ms378484.aspx(以及許多其他),我試圖改變連接池的定義:通過GlassFish管理控制檯
Connection Pool:
Name: BEACHWATER_LIMS
Datasource Classname: com.microsoft.sqlserver.jdbc.SQLServerXADataSource
Resource Type: javax.sql.XADataSource
Ping成功,但打電話給analysisService。 getAllAnalysisTypes()現在拋出異常:
Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: javax.transaction.SystemException
The resource manager is doing work outside a global transaction javax.transaction.xa.XAException: com.microsoft.sqlserver.jdbc.SQLServerException: Failed to create the XA control connection. Error: "Could not find stored procedure 'master..xp_sqljdbc_xa_init_ex'."
任何想法?
可能的重複[如何防止「本地事務已經有1個非XA資源」異常?](https://stackoverflow.com/questions/2687121/how-to-prevent-local-transaction-already-has -1-non-xa-resource-exception) – 2017-08-26 14:33:37