ssh配置多个数据源
SSH配置多个数据源
1. 编写hibernate的数据库配置文
Mysql_hibernate.cfg.xml代码
1.<?xml version='1.0' encoding='UTF-8'?>
2.<!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
4.
5.<hibernate-configuration>
6. <session-factory>
7. <property name="show_sql">true</property>
8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
9. <property name="connection.url">jdbc:mysql://172.16.11.9:3306/mytest?useUnicode=true&characterEncoding=utf-8</property>
10. <property name="connection.username">root</property>
11. <property name="connection.password">rootroot</property>
12.
13. <property name="hibernate.c3p0.max_size">20</property>
14. <property name="hibernate.c3p0.min_size">1</property>
15. <property name="hibernate.c3p0.timeout">5000</property>
16. <property name="hibernate.c3p0.max_statements">100</property>
17. <property name="hibernate.c3p0.idle_test_period">3000</property>
18. <property name="hibernate.c3p0.acquire_increment">2</property>
19. <!--
20. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
21. -->
22. <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
23. <property name="hbm2ddl.auto">create</property>
24.
25. <property name="hibernate.current_session_context_class">thread</property>
26.
27.
28. <mapping resource="com/test/bean/User.hbm.xml"/>
29. </session-factory>
30.</hibernate-configuration>
Sqlserver_hibernate.cfg.xml代码
1.<?xml version='1.0' encoding='UTF-8'?>
2.<!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6.<hibernate-configuration>
7. <session-factory>
8. <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
9. <property name="connection.url">jdbc:microsoft:sqlserver://172.16.11.20:1433;DatabaseName=new_cmcc;SelectMethod=cursor</property>
10. <property name="connection.username">sa</property>
11. <property name="connection.password">111</property>
12. <property name="connection.pool_size">100</property>
13. <property name="show_sql">true</property>
14. <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
15.
16. <mapping resource="com/test/bean/User.hbm.xml"/>
17. </session-factory>
18.</hibernate-configuration>
Oracle_hibernate.cfg.xml代码
1.<?xml version='1.0' encoding='UTF-8'?>
2.<!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6.<hibernate-configuration>
7. <session-factory>
8. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
9. <property name="connection.url">jdbc:oracle:thin:@172.16.11.14:1521:epip</property>
10. <property name="connection.username">capitel</property>
11. <property name="connection.password">123456</property>
12. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
13. <property name="show_sql">false</property>
14.
15. <mapping resource="com/test/bean/User.hbm.xml"/>
16. </session-factory>
17.</hibernate-configuration>
2. 在spring配置文件(applicationContext.xml)中配置Hibernate数据源(以测试过的三个数据库oralce,mysql,server sql为例)
Applicationcontext.xml代码
1.<!-- My Sql -->
2.<bean id="mySqlSessionFactory" />
10. </property>
11.</bean>
12.
13.<!-- SQL Server -->
14.<bean id="sqlServerSessionFactory" />
22. </property>
23.</bean>
24.
25.<!-- Oracle -->
26.<bean id="oracleSessionFactory" />
34. </property>
35.</bean>
3. 在spring配置文件(applicationContext.xml)中配置spring的事务拦截器(AOP):
Applicationcontext.xml代码
1.<!--1、mysql数据源 事务管理拦截器-->
2. <tx:advice id="mySqlTxAdvice" transaction-manager="mySqlTransactionManager">
3. <tx:attributes>
4. <tx:method name="find*" read-only="true"/>
5. <tx:method name="get*" read-only="true"/>
6. <tx:method name="notx*" propagation="NEVER"/>
7. <tx:method name="*" rollback-for="自定义Exception"/>
8. </tx:attributes>
9. </tx:advice>
10.<!--2、sqlserver数据源 事务管理拦截器 -->
11. <tx:advice id="sqlServerTxAdvice" transaction-manager="sqlServerTransactionManager">
12. <tx:attributes>
13. <tx:method name="find*" read-only="true"/>
14. <tx:method name="get*" read-only="true"/>
15. <tx:method name="notx*" propagation="NEVER"/>
16. <tx:method name="*" rollback-for="自定义Exception"/>
17. </tx:attributes>
18. </tx:advice>
19.
20.<!--3、oracle数据源 事务管理拦截器-->
21. <tx:advice id="oracleTxAdvice" transaction-manager="oracleTransactionManager">
22. <tx:attributes>
23. <tx:method name="find*" read-only="true"/>
24. <tx:method name="get*" read-only="true"/>
25. <tx:method name="notx*" propagation="NEVER"/>
26. <tx:method name="*" rollback-for="自定义Exception"/>
27. </tx:attributes>
28. </tx:advice>
29.
30.<!--配置多数据源的事务拦截器-->
31. <bean id="serviceAutoProxyCreator" scope="singleton">
2. <property name="sessionFactory">
3. <ref bean="oracleSessionFactory"/>
4. </property>
5.</bean>
6.
7.<bean id="emplyDAO" scope="singleton">
8. <property name="sessionFactory">
9. <ref bean="mySqlSessionFactory"/>
10. </property>
11.</bean>