jpa – persistence. XML is used for multiple persistence units

I try to hold the same entity in MySQL and Postgres databases (this is mainly to identify any inconsistencies and explain the details of any problems with double writing - I have discussed it here) The articles I found describe solutions that rely on other frameworks I tried to use GlassFish 4.0 out of the box, JPA 2.1 and eclipse link 2.5 as JPA providers to solve this problem I'm using eclipse and realize that the IDE doesn't support in persistence Multiple persistence units are configured in the XML file, so I'm writing XML directly

I expect to do this in code (in the same method):

@PersistenceContext(name = "MyAppMysqLPU")
EntityManager emMysqL;
@PersistenceContext(name = "MyAppPostgresPU")
EntityManager emPostgres;
//...etc...
MyThing thing = new MyThing();
//...etc...
emMysqL.persist(thing);
emPostgres.persist(thing);

And use persistence. With the following XML file:

<persistence-unit name="MyAppPostgresPU">
    <jta-data-source>jdbc/PostgresPool_test</jta-data-source>
    <class>model.MyThing</class>
  </persistence-unit>

  <persistence-unit name="MyAppMysqLPU">
    <jta-data-source>jdbc/MysqLPool_test</jta-data-source>
    <class>model.MyThing</class>
  </persistence-unit>

When I do this, I get the following errors:

SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
SEVERE: Exception while preparing the app
SEVERE: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [MyAppPostgresPU] in the scope of the module called [MyApp]. Please verify your application.

However, if I only include the < persistence unit > phrase (which doesn't matter), the entity is persisted to the associated database - I just can't figure out how to make it handle both at the same time (I don't need to take advantage of persistence in an additional framework)

Solution

Get its job; There are several things that must be done This seems to be a key part. In order to use my method to use multiple databases, the connection pool type needs to be set to use distributed transactions Since this is basically an experiment, database persistence does not need to be in the same transaction, but it is not a problem This article helps to identify the error message You also need to change the Postgres parameter as described here to make the prepared transaction possible

All this:

(1) In GlassFish: @ h_ 419_ 23 @ in the JDBC connection pool, change the resource type of DB to javax sql. XADataSource. Change the datasource class name of Postgres to org postgresql. xa. PGXADataSource; Change datasource classname of Mysql to com MysqL. jdbc. jdbc2. optional. MysqLXADataSource.

(2) In posgres configuration (PostgreSQL. Config): @ h_ 419_ 23 @ enable Max_ prepared_ Transactions and set it to greater than max_ connections 1. (I have to use two parameters to find something that doesn't blow out all the available shared memory; but since this is just an experiment, reducing the number of database connections and increasing the shared memory can)

(3) In Code: @ h_ 419_ 23 @ change @ persistencecontext (name = ""... ") to @ persistencecontext (unitname =" "...")

Warning about this "answer" – most are new to me, so this may not be the most elegant way to deal with this problem If someone can provide "best practices" to solve this problem, I will be interested to know

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>