-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Using multiple connection pools in Hibernate
PostPosted: Tue Apr 17, 2012 12:37 am 
Newbie

Joined: Mon Apr 16, 2012 11:52 pm
Posts: 3
Hi all,

I'm trying to find a way I can use multiple connection pools.

Use Case:
I have 1 write master database and 5 readonly databases. So I what to be able to use the write connection pool for writing and the read connection pools for reading. When reading then any of the read connection pools could be used (based on load etc).

Does anyone know how to do this? Be nice to just pass a connection object into hibernate...Could it be that easy??

Thanks
Stephen


Top
 Profile  
 
 Post subject: Re: Using multiple connection pools in Hibernate
PostPosted: Tue Apr 17, 2012 5:21 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
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#p2452561

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


Top
 Profile  
 
 Post subject: Re: Using multiple connection pools in Hibernate
PostPosted: Tue Apr 17, 2012 2:10 pm 
Newbie

Joined: Mon Apr 16, 2012 11:52 pm
Posts: 3
Thanks. Is there a way to do this dynamically ? As the number of read databases could grow and shrink depending on load.

Thanks
Stephen


Top
 Profile  
 
 Post subject: Re: Using multiple connection pools in Hibernate
PostPosted: Wed Sep 02, 2015 1:24 am 
Newbie

Joined: Wed Sep 02, 2015 1:22 am
Posts: 1
How to do the same when using spring


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.