Saturday, June 1, 2013

Two Datasource in Spring/Hiberate

We can create two datasource in a single spring/hibernate application.

There are following steps required to implement this:-

Create JNDI Datasource while using Tomcat :- 
  1. Resource entry in server.xml under <GlobalNamingResources> for both datasources
    You can find server.xml in following path in Tomcat folder :-
     c://Tomcat/conf/server.xml
  2. <globalnamingresources>
        <resource name="jdbc/first"  driverclassname="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxwait="5000" password="makecodeeasy" type="javax.sql.DataSource" url="jdbc:sqlserver://localhost:1433;DatabaseName=DataTest" username="makecodeeasy" validationquery="select 1">
        </resource>
    
     <resource name="jdbc/second"  driverclassname="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxwait="5000" password="123" type="javax.sql.DataSource" url="jdbc:sqlserver://192.182.21.66:1433;DatabaseName=Second" username="sa" validationquery="select 1">
        </resource>
     </globalnamingresources>
    
    Note :- Database used here in MS Sql server 2008. You can use any database. You need to change driverClassname, url, username and password according to  database.
  3. Do the entry in web.xml for both resources
    You can find web.xml in WEB-INF folder of your application
  4. <resource-ref>
      <description>DB Connection Pooling</description> 
      <res-ref-name>java:comp/env/jdbc/first</res-ref-name> 
      <res-type>javax.sql.DataSource</res-type> 
      <res-auth>Container</res-auth> 
      <res-sharing-scope>Shareable</res-sharing-scope> 
    </resource-ref>
    
    
    <resource-ref>
      <description>DB Connection Pooling</description> 
      <res-ref-name>java:comp/env/jdbc/second</res-ref-name> 
      <res-type>javax.sql.DataSource</res-type> 
      <res-auth>Container</res-auth> 
      <res-sharing-scope>Shareable</res-sharing-scope> 
    </resource-ref>
    
  5. Use datasource in applicationContext or spring context file
  6. <bean id="firstDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="java:comp/env/jdbc/first" /> 
      <property name="resourceRef" value="true" /> 
    </bean>
    <bean id="secondDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="java:comp/env/jdbc/second" /> 
      <property name="resourceRef" value="true" /> 
    </bean> 
  7. Create two session factory for using two datasource.
    <bean id="firstSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="firstDataSource" p:configLocation="${hib.config}" p:packagesToScan="com.org.xyz.domain">
    </bean>
    <bean id="secondSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="secondDataSource" p:configLocation="${hib.config}" p:packagesToScan="com.org.abc.domain">
    </bean>

  8. Now these session factories can be used in Hibernate application.

ShareThis