SSH配置多个数据源
1. 编写hibernate的数据库配置文
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://172.16.11.9:3306/mytest?useUnicode=true&characterEncoding=utf-8</property>
- <property name="connection.username">root</property>
- <property name="connection.password">rootroot</property>
- <property name="hibernate.c3p0.max_size">20</property>
- <property name="hibernate.c3p0.min_size">1</property>
- <property name="hibernate.c3p0.timeout">5000</property>
- <property name="hibernate.c3p0.max_statements">100</property>
- <property name="hibernate.c3p0.idle_test_period">3000</property>
- <property name="hibernate.c3p0.acquire_increment">2</property>
- <!--
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- -->
- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
- <property name="hbm2ddl.auto">create</property>
- <property name="hibernate.current_session_context_class">thread</property>
- <mapping resource="com/test/bean/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
- <property name="connection.url">jdbc:microsoft:sqlserver://172.16.11.20:1433;DatabaseName=new_cmcc;SelectMethod=cursor</property>
- <property name="connection.username">sa</property>
- <property name="connection.password">111</property>
- <property name="connection.pool_size">100</property>
- <property name="show_sql">true</property>
- <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
- <mapping resource="com/test/bean/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <property name="connection.url">jdbc:oracle:thin:@172.16.11.14:1521:epip</property>
- <property name="connection.username">capitel</property>
- <property name="connection.password">123456</property>
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="show_sql">false</property>
- <mapping resource="com/test/bean/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
2. 在spring配置文件(applicationContext.xml)中配置Hibernate数据源(以测试过的三个数据库oralce,mysql,server sql为例)
- <!-- My Sql -->
- <bean id="mySqlSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>WEB-INF/classes/mysql_hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="mySqlTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="mySqlSessionFactory" />
- </property>
- </bean>
- <!-- SQL Server -->
- <bean id="sqlServerSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>WEB-INF/classes/sqlserver_hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="sqlServerTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sqlServerSessionFactory" />
- </property>
- </bean>
- <!-- Oracle -->
- <bean id="oracleSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>WEB-INF/classes/oracle_hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="oracleTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="oracleSessionFactory" />
- </property>
- </bean>
3. 在spring配置文件(applicationContext.xml)中配置spring的事务拦截器(AOP):
- <!--1、mysql数据源 事务管理拦截器-->
- <tx:advice id="mySqlTxAdvice" transaction-manager="mySqlTransactionManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true"/>
- <tx:method name="get*" read-only="true"/>
- <tx:method name="notx*" propagation="NEVER"/>
- <tx:method name="*" rollback-for="自定义Exception"/>
- </tx:attributes>
- </tx:advice>
- <!--2、sqlserver数据源 事务管理拦截器 -->
- <tx:advice id="sqlServerTxAdvice" transaction-manager="sqlServerTransactionManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true"/>
- <tx:method name="get*" read-only="true"/>
- <tx:method name="notx*" propagation="NEVER"/>
- <tx:method name="*" rollback-for="自定义Exception"/>
- </tx:attributes>
- </tx:advice>
- <!--3、oracle数据源 事务管理拦截器-->
- <tx:advice id="oracleTxAdvice" transaction-manager="oracleTransactionManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true"/>
- <tx:method name="get*" read-only="true"/>
- <tx:method name="notx*" propagation="NEVER"/>
- <tx:method name="*" rollback-for="自定义Exception"/>
- </tx:attributes>
- </tx:advice>
- <!--配置多数据源的事务拦截器-->
- <bean id="serviceAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>*Service</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>mySqlTxAdvice</value>
- <value>sqlServerTxAdvice</value>
- <value>oracleTxAdvice</value>
- </list>
- </property>
- </bean>
4. DAO需要使用哪个数据,就配置相应的sessionFactory,如:
- <bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
- <property name="sessionFactory">
- <ref bean="oracleSessionFactory"/>
- </property>
- </bean>
- <bean id="emplyDAO" class=" com.test.dao.dao.impl.Pb_EmplyDAOImpl" scope="singleton">
- <property name="sessionFactory">
- <ref bean="mySqlSessionFactory"/>
- </property>
- </bean>