JAVA spring hibernate 多数据源配置记录

  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不会生成其动态代理。
时间: 2024-12-19 08:14:14

JAVA spring hibernate 多数据源配置记录的相关文章

Java spring mvc多数据源配置

1.首先配置两个数据库 <bean id="dataSourceA"         class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <proper

spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置

先从persistence.xml开始: <?xml version=”1.0″ encoding=”UTF-8″?><persistence version=”2.1″ xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence

spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池

接上一个博文(http://www.loveweir.com/html/18.html),没有数据库连接池,纯粹用jpa的官方链接. 所以这次要加上连接池本文用Druid连接池来实现多数据源的配置. persistence.xml 这个文件可以省略了,全部配置在applicationContext.xml 里面: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www

spring 4+ jpa(hibernate 3/4)+spring mvc 多数据源配置(2)

接上一个博文,没有数据库连接池,纯粹用jpa的官方链接. 所以这次要加上连接池本文用Druid连接池来实现多数据源的配置. persistence.xml 这个文件可以省略了,全部配置在applicationContext.xml 里面: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"

Spring Boot 多数据源配置

下面一个Java类是已经写好的根据配置文件动态创建多dataSource的代码,其原理也很简单,就是读取配置文件,根据配置文件中配置的数据源数量,动态创建dataSource并注册到Spring中. 代码如下: package org.springboot.sample.config; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.sql.DataSource; i

spring boot 多数据源配置(多种数据库)

最近一段时间在使用spring boot开发项目,其中有一个项目用到了多数据源的配置,网上的资料还是不太多,走了好多才找到一个合适的,把自己写的分享一下,做个笔记,以后也许有用,第一次写博客,不好勿喷!! 首先介绍下我的业务场景,此项目用到了两种数据库,一个是mysql,另一个是sqlserver, 首先第一步需要在application.yml中将多数据源的配置信息进行配置, mysql数据源: spring: datasource: driverClassName: com.mysql.jd

spring多个数据源配置

sys.properties中的内容 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver DB.url=jdbc\:oracle\:thin\:@****\:1521\:**** DB.username=**** DB.password=**** jdbc.driverClassName2=oracle.jdbc.driver.OracleDriver DB.url2=jdbc\:oracle\:thin\:@****\:1521\:****

Spring+Mybatis多数据源配置

一.配置文件 properties ds1.driverClassName=com.mysql.jdbc.Driver ds1.url=jdbc:mysql://192.168.200.130:3306/test1?useUnicode=true&characterEncoding=UTF-8 ds1.username=hhh ds1.password=123456 ds2.driverClassName=oracle.jdbc.OracleDriver ds2.url=jdbc:mysql:/

IT忍者神龟之Spring+MyBatis多数据源配置实现

最近用到了MyBatis配置多数据源,原以为简单配置下就行了,实际操作后发现还是要费些事的,这里记录下,以作备忘 不多废话,直接上代码,后面会有简单的实现介绍 jdbc和log4j的配置 #定义输出格式 ConversionPattern=%d %-5p [%t] %c - %m%n log4j.rootLogger=DEBUG,Console log4j.logger.com.cnblogs.lzrabbit=DEBUG log4j.logger.org.springframework=ERR