Hi,
on possible solution would be to use multiple persistence-unit's (JPA approach), in other words using a separate persistence-unit for each database.
As connection pool I suggest the Tomcat JDBC Connection Pool, because it allows multiple instances and gives the possibility to excplicitly name them.
see
https://forum.hibernate.org/viewtopic.php?f=1&t=1014434&p=2452561#p2452561The persistence.xml would be something like this:
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="write">
<properties>
<property name="hibernate.connection.url" value="urltoWriterDatabase"/>
...
<property name="hibernate.connection.pool_size" value="80"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.TomcatJDBCConnectionProvider"/>
<property name="hibernate.tomcatJdbcPool.name" value="PoolWriterDatabase"/>
</properties>
</persistence-unit>
<persistence-unit name="reader1">
<properties>
<property name="hibernate.connection.url" value="urltoReaderDatabase1"/>
...
<property name="hibernate.connection.pool_size" value="80"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.TomcatJDBCConnectionProvider"/>
<property name="hibernate.tomcatJdbcPool.name" value="PoolReaderDatabase1"/>
</properties>
</persistence-unit>
...
</persistence>
The in your app you inititialize the different entitymanagerfactories:
Code:
EntityManagerFactory emfw= javax.persistence.Persistence.createEntityManagerFactory("write");
EntityManagerFactory emfreader1= javax.persistence.Persistence.createEntityManagerFactory("reader1");
EntityManager emw = emfw.createEntityManager();
emw .getTransaction().begin();
...
EntityManager emr1 = emfreader1.createEntityManager();
emfreader1.createNativeQuery("select c.* from Company c", Company.class).getResultList();
...
N.B.: I think the same should works also with using c3po connection pool, but to have different names, you have to use the JTA approach (defining DataSources with different names).