简体   繁体   中英

How to get EntityManager(s) for mulitple persistence units

I am writing a JEE7 application that runs in WebSphere Liberty Profile 8.5.5. We are using JPA (which is implemented via Eclipselink in WLP).

I have multiple persistence units in the same 'persistence.xml' file. 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:

@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?

This issue is prompting because one of your datasource configured as (single phase commit) using ConnectionPoolDataSource and other is configured with 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". 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

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.

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

A global transaction is whenever you issue a UserTransaction.begin() and lasts until you issue a commit() or a 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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