Search code examples
javahibernatetomcatmultiple-databases

hibernate and tomcat - multiple databases, how many listeners?


Here is the background: I have a need to connect to a number of DBs, and after reading the post on How to connect to multiple databases in Hibernate, I decided to give it a try, and wrote 3 "HibernateListener", each containing one SessionFactory, each corresponding to a hibernate-*.cfg.xml. However, it seems that only one of these "HibernateListeners" remains active after their initialisation. I suspect that the problem comes from declaring 3 listeners in web.xml, one for each "HibernateService":

<listener>  
  <listener-class>org.mypackage.HibernateListener1</listener-class>  
</listener>
<listener>  
  <listener-class>org.mypackage.HibernateListener2</listener-class>  
</listener>
<listener>  
  <listener-class>org.mypackage.HibernateListener3</listener-class>  
</listener>

Is this even allowed or do I have to cram my SessionFactories into one listener ?

Infos: Tomcat 7, Hibernate 3.5.6 (using annotations) Probably unneeded: mysql-connector java 5.1.23, MySQL 5.1.69. Also: no Spring


Solution

  • Well, problem came from another place. So to answer the question: Yes, one can have many listeners. If you decide to have all your SessionFactory's in one Listener, be careful to close them in the reverse order they were opened (if you open A,B & C, close C, B & A).