9

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

在persistence.xml中定义了2个持久化单元,如下:

<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 个无状态会话 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'."

有任何想法吗?

4

2 回答 2

7

在 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。

于 2010-04-15T11:13:00.243 回答
5

为了在事务中使用两个持久性单元(因此是两个数据源),您确实需要使用 XA 连接并相应地配置您的池(至少其中一个,GlassFish 支持最后一个代理优化,允许登记一个非 XA资源,请参阅http://docs.sun.com/app/docs/doc/820-7695/beanm?a=view)。这是第一个错误。

对于第二个错误,以目前的细节水平似乎很难说什么。您能否提供堆栈跟踪(如果需要,激活更精细的日志记录)?

于 2010-04-10T05:02:58.077 回答