Message Boards

Multiple DB connections to a single portlet

Kevin Neibarger, modified 2 Years ago.

Multiple DB connections to a single portlet

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts

Hello, I'm currently running the following Liferay Tomcat bundle

- liferay-ce-portal-7.3.6-ga7 + Tomcat 9.0.40

I'm currently converting from our 6.2 implementation. In our current implementation we have about 10-11 portlets which remain consistent over multiple clients and each client has their own database. I was able to simply get ahold of the HibernateSession and switch the datasource using the persistence-class attribute of the entity

<entity local-service= "true" name="MyTable" table="MyTable" 

data-source="defaultDataSource" 

persistence-class="com.healthmap.portlet.compass.custom_persistence.CustomPersistenceImpl$MyTableSessionOverride"

remote-service= "false" uuid= "false">

The CustomPersistenceImpl class was a Class of nested static classes which would extend the *PersistenceImpl classes and override the openSession() method. The openSession method would call a helper class which would retrieve a bean from ext-spring.xml that built a hibernate session for the given specified client connection

<bean id="otherDBSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"

  factory-method="newBean">

  <constructor-arg

   value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl" />

  <constructor-arg>

   <map>

    <entry key="dataSource" value-ref="otherDBDataSource" />

    <entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />

    <entry key="sessionFactoryImplementor" value-ref="otherDBHibernateSessionFactory" />

   </map>

  </constructor-arg>

Now, this implementation is not working in Liferay 7.3. The following classes appeared to be removed with no replacements

com.liferay.portal.kernel.spring.util.SpringFactoryUtil

com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl

com.liferay.portal.spring.hibernate.PortletHibernateConfiguration

My question is this, if I have one portlet that potentially uses multiple databases determined by a flag (or when user logs in) how can I switch the DB connection? I don't need one portlet to use multiple DB's I just need to switch the DB connection on command. Is there a way to do that?