Multiple database connection in Spring MVC 3

14 views
Skip to first unread message

Lei

unread,
Dec 28, 2012, 4:07:34 PM12/28/12
to
Need to make spring mvc work with different database. 
figure out if multiple transaction manager needed to be created for each data source.
spring dynamic datasource routing-(check the relative articles in spring official blog)
test with rrt application.
check this article for solutions:



Lei

unread,
Jan 3, 2013, 3:59:59 PM1/3/13
to programmin...@googlegroups.com
One of the solution is to set up multiple datasources and different transaction Managers for each source. Here is the example:


xsi:schemaLocation="
 
 <context:annotation-config />
 <context:component-scan base-package="your bean class" />

 <tx:annotation-driven transaction-manager="transactionManager" /> 
 
 <bean id = "XXX"
      class = "yourclass.XXX"></bean>
<!-- Declare a datasource that has pooling capabilities-->  
<bean id="dataSource1"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url" value="your first database"></property>
<property name="username" value="aaaaa"></property>
<property name="password" value="aaaaa"></property>
</bean>
       <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource1" />
          
          
      
      <bean id="dataSource2"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url" value="your second data source"></property>
<property name="username" value="bbbbb"></property>
<property name="password" value="bbbbb"></property>
</bean>
       <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource2" />
      
      
    
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
 
</beans>

Lei

unread,
Jan 3, 2013, 4:34:08 PM1/3/13
to
AbstractRoutingDataSource is still worth reviewing for its class and know how to use it.
Difference between these two methods?

temp.txt
Reply all
Reply to author
Forward
0 new messages