简体   繁体   English

如何获取多个持久性单元的EntityManager

[英]How to get EntityManager(s) for mulitple persistence units

I am writing a JEE7 application that runs in WebSphere Liberty Profile 8.5.5. 我正在编写在WebSphere Liberty Profile 8.5.5中运行的JEE7应用程序。 We are using JPA (which is implemented via Eclipselink in WLP). 我们正在使用JPA(通过WLP中的Eclipselink实现)。

I have multiple persistence units in the same 'persistence.xml' file. 我在同一个“ persistence.xml”文件中有多个持久性单元。 I also need to access two of those units in the same class. 我还需要访问同一课程中的其中两个单元。

I am getting a runtime error when I try to use the second EntityManager: 尝试使用第二个EntityManager时出现运行时错误:

@PersistenceContext(unitName = "wwer-list")
private EntityManager entityManagerWwerList;
@PersistenceContext(unitName = "main-dashboard")
private EntityManager entityManagerMainDashboard;

E WTRN0062E: An illegal attempt to use multiple resources that have only one-phase capability has occurred within a global transaction.  

How do I get rid of this error? 我如何摆脱这个错误?

Also, all of the tables I am using are only needed for reading. 另外,我正在使用的所有表格仅需要阅读。 So how can I specify that I only want read-only access to JPA? 那么,如何指定我只希望对JPA具有只读访问权限?

This issue is prompting because one of your datasource configured as (single phase commit) using ConnectionPoolDataSource and other is configured with XADataSource. 出现此问题的原因是,您的一个数据源使用ConnectionPoolDataSource配置为(单阶段提交),而另一个数据源则配置了XADataSource。

If you want to continue with the same datasource configuration, you will have to update your Server configuration to "Acccept Heuristic Hazard". 如果要继续使用相同的数据源配置,则必须将服务器配置更新为“接受启发式危害”。

In the admin console, click the EAR, select the check box "Accept heuristic hazard". 在管理控制台中,单击EAR,然后选中“接受启发式危害”复选框。 Re-start the server. 重新启动服务器。

This link to enable the Last Participant Support may also help. 启用最后参与者支持的此链接也可能会有所帮助。 http://www.ibm.com/support/knowledgecenter/SSAW57_7.0.0/com.ibm.websphere.nd.doc/info/ae/webui_pme/ui/ueac_laoextensionsettings.html http://www.ibm.com/support/knowledgecenter/SSAW57_7.0.0/com.ibm.websphere.nd.doc/info/ae/webui_pme/ui/ueac_laoextensionsettings.html

I can't tell for sure without your persistence.xml and server.xml configurations, but it looks like the <dataSource> elements backing your <persistence-unit> configurations are not XA capable. 如果没有您的persistence.xml和server.xml配置,我无法确定,但是看起来支持<persistence-unit>配置的<dataSource>元素不具有XA功能。

By default, a <dataSource> should be a javax.sql.XADataSource (and therefore XA capable), however if you are using a JDBC driver that does not provide an XADataSource implementation, Liberty will pick a simpler DataSource implementation (ie javax.sql.ConnectionPoolDataSource or plain javax.sql.DataSource ). 默认情况下, <dataSource>应该是javax.sql.XADataSource (因此具有XA功能),但是,如果使用的是不提供XADataSource实现的JDBC驱动程序,Liberty将选择一个更简单的DataSource实现(即javax.sql.ConnectionPoolDataSource或纯javax.sql.DataSource )。

A global transaction is whenever you issue a UserTransaction.begin() and lasts until you issue a commit() or a rollback() . 全局事务在您发出UserTransaction.begin()一直持续到您发出commit()rollback()为止。 There are other ways that you can get into a global transaction too. 您还可以通过其他方式进行全球交易。

Since you want read-only access, converting your DataSources to XA would probably be overkill. 由于您需要只读访问权限,因此将DataSources转换为XA可能会过大。 Instead, try to eliminate the global transactions from the equation. 相反,尝试从等式中消除全局事务。 If you can't eliminate the global transactions, you can specify XADataSource in your server.xml in the following way: 如果无法消除全局事务,则可以通过以下方式在server.xml中指定XADataSource:

<dataSource type="javax.sql.XADataSource" ...>
    <jdbcDriver .../>
    <properties .../>
</dataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM