gpt4 book ai didi

transactions - EJB3 - 在事务中使用 2 个持久性单元(异常 : Local transaction already has 1 non-XA Resource)

转载 作者:行者123 更新时间:2023-12-01 17:05:21 29 4
gpt4 key购买 nike

我尝试在部署在 Glassfish 上的 Java EE 应用程序的同一事务中使用 2 个持久性单元。

这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>
...

这些持久性单元对应于我在 Glassfish 中定义的 JDBC 资源和连接池,如下所示(此处包括一个,因为除了名称和数据库连接信息之外,两者都是相同的):

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 个无状态 session 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 (除其他外),我尝试将连接池的定义更改为:

Connection Pool:
Name: BEACHWATER_LIMS
Datasource Classname: com.microsoft.sqlserver.jdbc.SQLServerXADataSource
Resource Type: javax.sql.XADataSource

通过 Glassfish 管理控制台 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'."

有什么想法吗?

最佳答案

更改 Glassfish 中连接池的配置:

Connection Pool:
Name: BEACHWATER_LIMS
Datasource Classname: com.microsoft.sqlserver.jdbc.SQLServerXADataSource
Resource Type: javax.sql.XADataSource

按照 Senthil Balakrishnan 博客“如何使 MSSQL Server XA 数据源工作?”中的步骤进行操作在这里,http://www.senthilb.com/2010/01/how-to-make-xa-datasource-work-in-mssql.html .

重新启动 Glassfish。

关于transactions - EJB3 - 在事务中使用 2 个持久性单元(异常 : Local transaction already has 1 non-XA Resource),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2606497/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com