persistence.xml for multiple persistence units

你。 提交于 2019-12-03 03:27:33

Got it working; several things had to be done. It seems a key part of this is that in order to use the multiple databases with the approach I was taking, the connection pool type needs to be set to use distributed transactions. Since this is essentially an experiment, both db persists didn't need to be in the same transaction, but it's not a problem for it to be that way. (This article was helpful in identifying that from the error messages). It was also necessary to change the Postgres parameters as described here, to enable prepared transactions.


This got it all going:

(1) In Glassfish:
In the JDBC Connection pools, change the resource type for both db's to javax.sql.XADataSource. Change Datasource Classname for Postgres to org.postgresql.xa.PGXADataSource; change Datasource Classname for MySQL to com.mysql.jdbc.jdbc2.optional.MysqlXADataSource.

(2) In Posgres configuration (postgresql.config):
Enable max_prepared_transactions and set it to be 1 greater than max_connections. (I had to play around with both parameters to find something that didn't blow out all available shared memory; but since this is only an experiment, decreasing number of db connections vs increasing shared memory is OK)

(3) In the code:
Change @PersistenceContext(name="...") to @PersistenceContext(unitName="...")


Caveat on this "answer" -- Most of this is new for me, so this may not be the most elegant way to handle this. If anyone can provide a "best practice" to solve this, I would be very interested to know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!