<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Multiple DB connections to a single portlet</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120807945" />
  <subtitle>Multiple DB connections to a single portlet</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120807945</id>
  <updated>2026-04-06T11:30:16Z</updated>
  <dc:date>2026-04-06T11:30:16Z</dc:date>
  <entry>
    <title>Multiple DB connections to a single portlet</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120807944" />
    <author>
      <name>Kevin Neibarger</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120807944</id>
    <updated>2021-05-27T19:08:03Z</updated>
    <published>2021-05-27T19:08:03Z</published>
    <summary type="html">&lt;p&gt;Hello, I'm currently running the following Liferay Tomcat bundle&lt;/p&gt;
&lt;p&gt;- liferay-ce-portal-7.3.6-ga7 + Tomcat 9.0.40&lt;/p&gt;
&lt;p&gt;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&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&amp;lt;&lt;b&gt;entity&lt;/b&gt; local-service=&lt;b&gt;
      &lt;i&gt;&amp;quot;true&amp;quot;&lt;/i&gt;&lt;/b&gt; name=&lt;i&gt;&amp;quot;MyTable&amp;quot;&lt;/i&gt; table=&lt;i&gt;&amp;quot;MyTable&amp;quot;&lt;/i&gt; &lt;/p&gt;
  &lt;p&gt;data-source=&lt;i&gt;&amp;quot;defaultDataSource&amp;quot;&lt;/i&gt; &lt;/p&gt;
  &lt;p&gt;persistence-class=&lt;i&gt;&amp;quot;com.healthmap.portlet.compass.custom_persistence.CustomPersistenceImpl$MyTableSessionOverride&amp;quot;&lt;/i&gt;&lt;/p&gt;
  &lt;p&gt;remote-service=&lt;b&gt;
      &lt;i&gt;&amp;quot;false&amp;quot;&lt;/i&gt;&lt;/b&gt; uuid=&lt;b&gt;
      &lt;i&gt;&amp;quot;false&amp;quot;&lt;/i&gt;&lt;/b&gt;&amp;gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The &lt;strong&gt;CustomPersistenceImpl&lt;/strong&gt; class was a Class of
  nested static classes which would extend the &lt;strong&gt;*PersistenceImpl
  &lt;/strong&gt;classes and override the openSession() method. The
  openSession method would call a helper class which would retrieve a
  bean from &lt;strong&gt;ext-spring.xml&lt;/strong&gt; that built a hibernate
  session for the given specified client connection&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&amp;lt;&lt;b&gt;bean&lt;/b&gt; id=&lt;i&gt;&amp;quot;otherDBSessionFactory&amp;quot;&lt;/i&gt; class=&lt;i&gt;&amp;quot;com.liferay.portal.kernel.spring.util.SpringFactoryUtil&amp;quot;&lt;/i&gt;&lt;/p&gt;
  &lt;p&gt;  factory-method=&lt;i&gt;&amp;quot;newBean&amp;quot;&lt;/i&gt;&amp;gt;&lt;/p&gt;
  &lt;p&gt;  &amp;lt;&lt;b&gt;constructor-arg&lt;/b&gt;&lt;/p&gt;
  &lt;p&gt;  
    value=&lt;i&gt;&amp;quot;com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl&amp;quot;&lt;/i&gt; /&amp;gt;&lt;/p&gt;
  &lt;p&gt;  &amp;lt;&lt;b&gt;constructor-arg&lt;/b&gt;&amp;gt;&lt;/p&gt;
  &lt;p&gt;   &amp;lt;&lt;b&gt;map&lt;/b&gt;&amp;gt;&lt;/p&gt;
  &lt;p&gt;    &amp;lt;&lt;b&gt;entry&lt;/b&gt; key=&lt;i&gt;&amp;quot;dataSource&amp;quot;&lt;/i&gt;
    value-ref=&lt;i&gt;&amp;quot;otherDBDataSource&amp;quot;&lt;/i&gt; /&amp;gt;&lt;/p&gt;
  &lt;p&gt;    &amp;lt;&lt;b&gt;entry&lt;/b&gt;
    key=&lt;i&gt;&amp;quot;sessionFactoryClassLoader&amp;quot;&lt;/i&gt;
    value-ref=&lt;i&gt;&amp;quot;portletClassLoader&amp;quot;&lt;/i&gt; /&amp;gt;&lt;/p&gt;
  &lt;p&gt;    &amp;lt;&lt;b&gt;entry&lt;/b&gt;
    key=&lt;i&gt;&amp;quot;sessionFactoryImplementor&amp;quot;&lt;/i&gt;
    value-ref=&lt;i&gt;&amp;quot;otherDBHibernateSessionFactory&amp;quot;&lt;/i&gt; /&amp;gt;&lt;/p&gt;
  &lt;p&gt;   &amp;lt;/&lt;b&gt;map&lt;/b&gt;&amp;gt;&lt;/p&gt;
  &lt;p&gt;  &amp;lt;/&lt;b&gt;constructor-arg&lt;/b&gt;&amp;gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, this implementation is not working in Liferay 7.3. The following
  classes appeared to be removed with no replacements&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;
    &lt;i&gt;com.liferay.portal.kernel.spring.util.SpringFactoryUtil&lt;/i&gt;&lt;/p&gt;
  &lt;p&gt;
    &lt;i&gt;com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl&lt;/i&gt;&lt;/p&gt;
  &lt;p&gt;
    &lt;i&gt;com.liferay.portal.spring.hibernate.PortletHibernateConfiguration&lt;/i&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;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?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>Kevin Neibarger</dc:creator>
    <dc:date>2021-05-27T19:08:03Z</dc:date>
  </entry>
</feed>
