码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA spring hibernate 多数据源配置记录

时间:2015-08-26 22:02:56      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

  1. 数据源配置
    技术分享
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
     5     default-autowire="byName">
     6     <bean id="dataSource-hr" class="com.alibaba.druid.pool.DruidDataSource"
     7         init-method="init" destroy-method="close">
     8         <property name="url" value="${jdbc.url}" />
     9         <property name="username" value="${jdbc.username}" />
    10         <property name="password" value="${jdbc.password}" />
    11 
    12         <property name="filters" value="stat" />
    13 
    14         <property name="maxActive" value="20" />
    15         <property name="initialSize" value="1" />
    16         <property name="maxWait" value="60000" />
    17         <property name="minIdle" value="1" />
    18 
    19         <property name="timeBetweenEvictionRunsMillis" value="60000" />
    20         <property name="minEvictableIdleTimeMillis" value="300000" />
    21 
    22         <property name="validationQuery" value="SELECT ‘x‘" />
    23         <property name="testWhileIdle" value="true" />
    24         <property name="testOnBorrow" value="false" />
    25         <property name="testOnReturn" value="false" />
    26 
    27         <property name="poolPreparedStatements" value="true" />
    28         <property name="maxPoolPreparedStatementPerConnectionSize"
    29             value="50" />
    30     </bean>
    31 
    32     <!-- Hibernate Jpa SessionFactory -->
    33     <bean id="sessionFactory"
    34         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    35         <property name="dataSource" ref="dataSource-hr" />
    36         <property name="hibernateProperties">
    37             <props>
    38                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    39                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    40                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    41                 <prop key="hibernate.jdbc.fetch_size">200</prop>
    42                 <prop key="hibernate.jdbc.batch_size">200</prop>
    43                 <prop key="javax.persistence.validation.mode">none</prop>
    44             </props>
    45         </property>
    46         <property name="packagesToScan">
    47             <list>
    48                 <value>com.xx.xx.pojo</value>
    49             </list>
    50         </property>
    51     </bean>
    52 </beans>
    数据源1配置
    技术分享
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
     5     default-autowire="byName">
     6     <bean id="dataSource-pku" class="com.alibaba.druid.pool.DruidDataSource"
     7         init-method="init" destroy-method="close">
     8         <property name="url" value="${jdbc.pku.url}" />
     9         <property name="username" value="${jdbc.pku.username}" />
    10         <property name="password" value="${jdbc.pku.password}" />
    11 
    12         <property name="filters" value="stat" />
    13 
    14         <property name="maxActive" value="20" />
    15         <property name="initialSize" value="1" />
    16         <property name="maxWait" value="60000" />
    17         <property name="minIdle" value="1" />
    18 
    19         <property name="timeBetweenEvictionRunsMillis" value="60000" />
    20         <property name="minEvictableIdleTimeMillis" value="300000" />
    21 
    22         <property name="validationQuery" value="SELECT ‘x‘" />
    23         <property name="testWhileIdle" value="true" />
    24         <property name="testOnBorrow" value="false" />
    25         <property name="testOnReturn" value="false" />
    26 
    27         <property name="poolPreparedStatements" value="true" />
    28         <property name="maxPoolPreparedStatementPerConnectionSize"
    29             value="50" />
    30     </bean>
    31 
    32     <!-- Hibernate Jpa SessionFactory -->
    33     <bean id="sessionFactory-pku"
    34         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    35         <property name="dataSource" ref="dataSource-pku" />
    36         <property name="hibernateProperties">
    37             <props>
    38                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    39                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    40                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    41                 <prop key="hibernate.jdbc.fetch_size">200</prop>
    42                 <prop key="hibernate.jdbc.batch_size">200</prop>
    43                 <prop key="javax.persistence.validation.mode">none</prop>
    44                 <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
    45                 <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
    46                 <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->
    47                 <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
    48                 <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
    49             </props>
    50         </property>
    51 
    52         <property name="packagesToScan">
    53             <list>
    54                 <value>com.xx.xx.pojo</value>
    55             </list>
    56         </property>
    57     </bean>
    58 </beans>
    数据源2配置

     

  2. 事务的配置
    技术分享
      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <beans xmlns="http://www.springframework.org/schema/beans"
      3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
      4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
      5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      6    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
      7    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
      8     default-autowire="byName">
      9     <!-- 事务管理器配置, Hibernate多数据源事务 -->
     10     <bean id="transactionManager-hr"
     11         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     12         <property name="sessionFactory" ref="sessionFactory" />
     13     </bean>
     14 
     15     <bean id="transactionManager-pku"
     16         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     17         <property name="sessionFactory" ref="sessionFactory-pku" />
     18     </bean>
     19 
     20     <!-- 1.使用annotation定义事务 -->
     21     <!-- proxy-target-class ture:动态代理(需cglib类库的支持) false:接口(Spring默认方式) -->
     22     <tx:annotation-driven transaction-manager="transactionManager-pku"
     23         proxy-target-class="true" />
     24     <tx:annotation-driven transaction-manager="transactionManager-hr"
     25         proxy-target-class="true" />
     26     <!-- proxy-target-class true:CGLIB代理 false:JDK代理 -->
     27     <aop:aspectj-autoproxy expose-proxy="true"
     28         proxy-target-class="true" />
     29 
     30     <!-- 2.声明式事务配置 -->
     31     <aop:config>
     32         <aop:pointcut id="serviceMethods-pku"
     33             expression="execution(* com.wisdombud.career.business.*.*(..)) || execution(* com.wisdombud.career.business.impl.*.*(..))" />
     34         <aop:pointcut id="serviceMethods-hr"
     35             expression="execution(* com.wisdombud.wisdomhr.srv.*.*(..)) || execution(* com.wisdombud.wisdomhr.srv.impl.*.*(..))
     36             ||execution(* com.wisdombud.wisdomhr.common.srv.*.*(..)) ||execution(* com.wisdombud.common.srv.*.*(..))" />
     37 
     38         <aop:advisor advice-ref="txAdvice-pku" pointcut-ref="serviceMethods-pku" />
     39         <aop:advisor advice-ref="txAdvice-hr" pointcut-ref="serviceMethods-hr" />
     40     </aop:config>
     41 
     42     <!-- 通知 默认事务管理器:transaction-manager="transactionManager" -->
     43     <tx:advice id="txAdvice-hr" transaction-manager="transactionManager-hr">
     44         <tx:attributes>
     45             <tx:method name="find*" read-only="true" />
     46             <tx:method name="page*" read-only="true" />
     47             <tx:method name="list*" read-only="true" />
     48             <tx:method name="get*" read-only="true" />
     49             <tx:method name="plot*" read-only="true" />
     50             <tx:method name="login*" propagation="REQUIRED" read-only="true" />
     51             <tx:method name="set*" propagation="REQUIRED"
     52                 rollback-for="common.toolkit.java.exception" />
     53             <tx:method name="start*" propagation="REQUIRED"
     54                 rollback-for="common.toolkit.java.exception" />
     55             <tx:method name="process*" propagation="REQUIRED"
     56                 rollback-for="common.toolkit.java.exception" />
     57             <tx:method name="audit*" propagation="REQUIRED"
     58                 rollback-for="common.toolkit.java.exception" />
     59             <tx:method name="add*" propagation="REQUIRED"
     60                 rollback-for="common.toolkit.java.exception" />
     61             <tx:method name="save*" propagation="REQUIRED"
     62                 rollback-for="common.toolkit.java.exception" />
     63             <tx:method name="update*" propagation="REQUIRED"
     64                 rollback-for="common.toolkit.java.exception" />
     65             <tx:method name="register*" propagation="REQUIRED"
     66                 rollback-for="common.toolkit.java.exception" />
     67             <tx:method name="del*" propagation="REQUIRED"
     68                 rollback-for="common.toolkit.java.exception" />
     69             <tx:method name="change*" propagation="REQUIRED"
     70                 rollback-for="common.toolkit.java.exception" />
     71             <tx:method name="*" read-only="true" />
     72         </tx:attributes>
     73     </tx:advice>
     74     <!-- 通知 默认事务管理器:transaction-manager="transactionManager" -->
     75     <tx:advice id="txAdvice-pku" transaction-manager="transactionManager-pku">
     76         <tx:attributes>
     77             <tx:method name="find*" read-only="true" />
     78             <tx:method name="page*" read-only="true" />
     79             <tx:method name="list*" read-only="true" />
     80             <tx:method name="get*" read-only="true" />
     81             <tx:method name="plot*" read-only="true" />
     82             <tx:method name="login*" propagation="REQUIRED" read-only="true" />
     83             <tx:method name="set*" propagation="REQUIRED"
     84                 rollback-for="common.toolkit.java.exception" />
     85             <tx:method name="start*" propagation="REQUIRED"
     86                 rollback-for="common.toolkit.java.exception" />
     87             <tx:method name="process*" propagation="REQUIRED"
     88                 rollback-for="common.toolkit.java.exception" />
     89             <tx:method name="audit*" propagation="REQUIRED"
     90                 rollback-for="common.toolkit.java.exception" />
     91             <tx:method name="add*" propagation="REQUIRED"
     92                 rollback-for="common.toolkit.java.exception" />
     93             <tx:method name="save*" propagation="REQUIRED"
     94                 rollback-for="common.toolkit.java.exception" />
     95             <tx:method name="update*" propagation="REQUIRED"
     96                 rollback-for="common.toolkit.java.exception" />
     97             <tx:method name="register*" propagation="REQUIRED"
     98                 rollback-for="common.toolkit.java.exception" />
     99             <tx:method name="del*" propagation="REQUIRED"
    100                 rollback-for="common.toolkit.java.exception" />
    101             <tx:method name="change*" propagation="REQUIRED"
    102                 rollback-for="common.toolkit.java.exception" />
    103             <tx:method name="*" read-only="true" />
    104         </tx:attributes>
    105     </tx:advice>
    106 </beans>
    事务配置

     

  3. 部分类的spring bean配置
    技术分享
    1     <bean id="pku-dic-Dao" class="com.wisdombud.career.dao.impl.DicCommonDaoImpl">
    2         <property name="sessionFactory" ref="sessionFactory-pku" />
    3     </bean>
    类配置

     

  4. 说明与坑
    1. 多数据源主要是数据库有多个,而业务上往往决定了需要同时进行两个数据库的操作,因此在配置的时候需要考虑清楚事务一致性需要在什么级别配置;
    2. spring集成hibernate进行事务配置的时候,离不开aop,而aop其实是方法级别的配置,针对某些方法进行切面。如果要切面的类继承了另外一个类,而且父类与子类不在一个包里面,那么在执行父类方法的时候需要aop的话,aop的整体配置pointcut的时候,需要将包含父类的规则包含进来,否则spring不会生成其动态代理。

JAVA spring hibernate 多数据源配置记录

标签:

原文地址:http://www.cnblogs.com/lzxianren/p/4761565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!