朝军 2012-03-13
SSH配置多个数据源
1.编写hibernate的数据库配置文
Mysql_hibernate.cfg.xml代码
1.<?xmlversion='1.0'encoding='UTF-8'?>
2.<!DOCTYPEhibernate-configurationPUBLIC
3."-//Hibernate/HibernateConfigurationDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
4.
5.<hibernate-configuration>
6.<session-factory>
7.<propertyname="show_sql">true</property>
8.<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
9.<propertyname="connection.url">jdbc:mysql://172.16.11.9:3306/mytest?useUnicode=true&characterEncoding=utf-8</property>
10.<propertyname="connection.username">root</property>
11.<propertyname="connection.password">rootroot</property>
12.
13.<propertyname="hibernate.c3p0.max_size">20</property>
14.<propertyname="hibernate.c3p0.min_size">1</property>
15.<propertyname="hibernate.c3p0.timeout">5000</property>
16.<propertyname="hibernate.c3p0.max_statements">100</property>
17.<propertyname="hibernate.c3p0.idle_test_period">3000</property>
18.<propertyname="hibernate.c3p0.acquire_increment">2</property>
19.<!--
20.<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
21.-->
22.<propertyname="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
23.<propertyname="hbm2ddl.auto">create</property>
24.
25.<propertyname="hibernate.current_session_context_class">thread</property>
26.
27.
28.<mappingresource="com/test/bean/User.hbm.xml"/>
29.</session-factory>
30.</hibernate-configuration>
Sqlserver_hibernate.cfg.xml代码
1.<?xmlversion='1.0'encoding='UTF-8'?>
2.<!DOCTYPEhibernate-configurationPUBLIC
3."-//Hibernate/HibernateConfigurationDTD3.0//EN"
4."http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6.<hibernate-configuration>
7.<session-factory>
8.<propertyname="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
9.<propertyname="connection.url">jdbc:microsoft:sqlserver://172.16.11.20:1433;DatabaseName=new_cmcc;SelectMethod=cursor</property>
10.<propertyname="connection.username">sa</property>
11.<propertyname="connection.password">111</property>
12.<propertyname="connection.pool_size">100</property>
13.<propertyname="show_sql">true</property>
14.<propertyname="dialect">org.hibernate.dialect.SQLServerDialect</property>
15.
16.<mappingresource="com/test/bean/User.hbm.xml"/>
17.</session-factory>
18.</hibernate-configuration>
Oracle_hibernate.cfg.xml代码
1.<?xmlversion='1.0'encoding='UTF-8'?>
2.<!DOCTYPEhibernate-configurationPUBLIC
3."-//Hibernate/HibernateConfigurationDTD3.0//EN"
4."http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6.<hibernate-configuration>
7.<session-factory>
8.<propertyname="dialect">org.hibernate.dialect.Oracle9Dialect</property>
9.<propertyname="connection.url">jdbc:oracle:thin:@172.16.11.14:1521:epip</property>
10.<propertyname="connection.username">capitel</property>
11.<propertyname="connection.password">123456</property>
12.<propertyname="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
13.<propertyname="show_sql">false</property>
14.
15.<mappingresource="com/test/bean/User.hbm.xml"/>
16.</session-factory>
17.</hibernate-configuration>
2.在spring配置文件(applicationContext.xml)中配置Hibernate数据源(以测试过的三个数据库oralce,mysql,serversql为例)
Applicationcontext.xml代码
1.<!--MySql-->
2.<beanid="mySqlSessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3.<propertyname="configLocation">
4.<value>WEB-INF/classes/mysql_hibernate.cfg.xml</value>
5.</property>
6.</bean>
7.<beanid="mySqlTransactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
8.<propertyname="sessionFactory">
9.<reflocal="mySqlSessionFactory"/>
10.</property>
11.</bean>
12.
13.<!--SQLServer-->
14.<beanid="sqlServerSessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
15.<propertyname="configLocation">
16.<value>WEB-INF/classes/sqlserver_hibernate.cfg.xml</value>
17.</property>
18.</bean>
19.<beanid="sqlServerTransactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
20.<propertyname="sessionFactory">
21.<reflocal="sqlServerSessionFactory"/>
22.</property>
23.</bean>
24.
25.<!--Oracle-->
26.<beanid="oracleSessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27.<propertyname="configLocation">
28.<value>WEB-INF/classes/oracle_hibernate.cfg.xml</value>
29.</property>
30.</bean>
31.<beanid="oracleTransactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
32.<propertyname="sessionFactory">
33.<reflocal="oracleSessionFactory"/>
34.</property>
35.</bean>
3.在spring配置文件(applicationContext.xml)中配置spring的事务拦截器(AOP):
Applicationcontext.xml代码
1.<!--1、mysql数据源事务管理拦截器-->
2.<tx:adviceid="mySqlTxAdvice"transaction-manager="mySqlTransactionManager">
3.<tx:attributes>
4.<tx:methodname="find*"read-only="true"/>
5.<tx:methodname="get*"read-only="true"/>
6.<tx:methodname="notx*"propagation="NEVER"/>
7.<tx:methodname="*"rollback-for="自定义Exception"/>
8.</tx:attributes>
9.</tx:advice>
10.<!--2、sqlserver数据源事务管理拦截器-->
11.<tx:adviceid="sqlServerTxAdvice"transaction-manager="sqlServerTransactionManager">
12.<tx:attributes>
13.<tx:methodname="find*"read-only="true"/>
14.<tx:methodname="get*"read-only="true"/>
15.<tx:methodname="notx*"propagation="NEVER"/>
16.<tx:methodname="*"rollback-for="自定义Exception"/>
17.</tx:attributes>
18.</tx:advice>
19.
20.<!--3、oracle数据源事务管理拦截器-->
21.<tx:adviceid="oracleTxAdvice"transaction-manager="oracleTransactionManager">
22.<tx:attributes>
23.<tx:methodname="find*"read-only="true"/>
24.<tx:methodname="get*"read-only="true"/>
25.<tx:methodname="notx*"propagation="NEVER"/>
26.<tx:methodname="*"rollback-for="自定义Exception"/>
27.</tx:attributes>
28.</tx:advice>
29.
30.<!--配置多数据源的事务拦截器-->
31.<beanid="serviceAutoProxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
32.<propertyname="beanNames">
33.<list>
34.<value>*Service</value>
35.</list>
36.</property>
37.<propertyname="interceptorNames">
38.<list>
39.<value>mySqlTxAdvice</value>
40.<value>sqlServerTxAdvice</value>
41.<value>oracleTxAdvice</value>
42.</list>
43.</property>
44.</bean>
4.DAO需要使用哪个数据,就配置相应的sessionFactory,如:
Java代码
1.<beanid="userDao"class="com.test.dao.impl.UserDAOImpl"scope="singleton">
2.<propertyname="sessionFactory">
3.<refbean="oracleSessionFactory"/>
4.</property>
5.</bean>
6.
7.<beanid="emplyDAO"class="com.test.dao.dao.impl.Pb_EmplyDAOImpl"scope="singleton">
8.<propertyname="sessionFactory">
9.<refbean="mySqlSessionFactory"/>
10.</property>
11.</bean>