Spring -- 多数据源配置

来源:互联网 发布:部落冲突 模拟软件 编辑:程序博客网 时间:2024/06/05 04:20

①单数据源配置


beans.xml的配置

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">      <property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>      <property name="jdbcUrl"><value>jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8</value></property>      <property name="user"><value>root</value></property>      <property name="password"><value>123456</value></property>      <!-- 连接池中保留的最小连接数。 -->      <property name="minPoolSize" value="5" />      <!-- 连接池中保留的最大连接数。Default: 15 -->      <property name="maxPoolSize" value="20" />      <!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->      <property name="maxIdleTime" value="1800" />      <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->      <property name="acquireIncrement" value="3" />      <property name="maxStatements" value="1000" />      <property name="initialPoolSize" value="10" />      <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->      <property name="idleConnectionTestPeriod" value="60" />      <!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->      <property name="acquireRetryAttempts" value="30" />      <property name="breakAfterAcquireFailure" value="false" />      <property name="testConnectionOnCheckout" value="false" />  </bean>

Dao.java

private static SessionFactory sessionFactory;     protected SessionFactory getSessionFactory(){      if (sessionFactory == null){          WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();          sessionFactory = (SessionFactory)context.getBean("sessionFactory");      }      return sessionFactory;  }


②多数据源配置


beans.xml

<!-- 配置数据源 --><bean id="dataSource01" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://127.0.0.1:3306/db_test_01?useUnicode=true&characterEncoding=UTF-8</value></property><property name="user"><value>root</value></property><property name="password"><value>123456</value></property><!-- 连接池中保留的最小连接数。 --><property name="minPoolSize" value="5" /><!-- 连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="20" /><!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800" /><!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3" /><property name="maxStatements" value="1000" /><property name="initialPoolSize" value="10" /><!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60" /><!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --><property name="acquireRetryAttempts" value="30" /><property name="breakAfterAcquireFailure" value="false" /><property name="testConnectionOnCheckout" value="false" /></bean><bean id="dataSource02" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://127.0.0.1:3306/db_test_02?useUnicode=true&characterEncoding=UTF-8</value></property><property name="user"><value>root</value></property><property name="password"><value>123456</value></property><!-- 连接池中保留的最小连接数。 --><property name="minPoolSize" value="5" /><!-- 连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="20" /><!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800" /><!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3" /><property name="maxStatements" value="1000" /><property name="initialPoolSize" value="10" /><!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60" /><!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --><property name="acquireRetryAttempts" value="30" /><property name="breakAfterAcquireFailure" value="false" /><property name="testConnectionOnCheckout" value="false" /></bean><!-- 配置sessionfactory --><bean id="sessionFactory01" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource01" /><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.hbm2ddl.auto=update<!-- hibernate.show_sql=true -->hibernate.format_sql=false</value></property><property name="configLocations"><list><value>classpath*:hibernate.cfg-01.xml</value></list></property></bean><bean id="sessionFactory02" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource02" /><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.hbm2ddl.auto=update<!-- hibernate.show_sql=true -->hibernate.format_sql=false</value></property><property name="configLocations"><list><value>classpath*:hibernate.cfg-02.xml</value></list></property></bean><!-- 配置事务管理器 --><bean id="transactionManager01" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory01" /></bean><tx:annotation-driven transaction-manager="transactionManager01" /><bean id="transactionManager02" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory02" /></bean><tx:annotation-driven transaction-manager="transactionManager02" />

Dao.java

private static SessionFactory sessionFactory01;private static SessionFactory sessionFactory02;protected SessionFactory getSessionFactory(String dataSource) {SessionFactory sessionFactory = null;if (dataSource.equals("dataSource01")) {if (sessionFactory01 == null) {WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();sessionFactory01 = (SessionFactory) context.getBean("sessionFactory01");}sessionFactory = sessionFactory01;} else if (dataSource.equals("dataSource02")) {if (sessionFactory02 == null) {WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();sessionFactory02 = (SessionFactory) context.getBean("sessionFactory02");}sessionFactory = sessionFactory02;}return sessionFactory;}


原创粉丝点击