HibernateはSpring構成に結合して動的に切り替えることができるデータソースです。
9160 ワード
HibernateはSpring構成と連動して動的に切り替わるデータソースであり、具体的な構成は以下の通りである。
(1)Springプロファイル
(1)Springプロファイル
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- -->
<bean id="databaseServer1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database1" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="databaseServer2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database2" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="databaseServer3"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database3" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="databaseCenter"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database_center" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<!-- spring -->
<bean id="databaseSource" class="com.ibsrapp.database.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="APPSERVER1" value-ref="databaseServer1"></entry>
<entry key="APPSERVER2" value-ref="databaseServer2"></entry>
<entry key="APPSERVER3" value-ref="databaseServer3"></entry>
<entry key="APPCENTER" value-ref="databaseCenter"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="databaseServer1">
</property>
</bean>
<!--Ibsrapp -->
<bean id="databaseSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="databaseSource"/>
<property name="mappingResources">
<list>
<value>com/ibsrapp/domain/dbtable.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<!-- -->
<prop key="hibernate.c3p0.max_size">30</prop>
<!-- -->
<prop key="hibernate.c3p0.min_size">5</prop>
<!-- , , , -->
<!-- <prop key="hibernate.c3p0.timeout">60000</prop> -->
<!-- PreparedStatement -->
<prop key="hibernate.c3p0.max_statements">100</prop>
<!-- 120 , -->
<prop key="hibernate.c3p0.idle_test_period">120</prop>
<!-- ,C3P0 -->
<prop key="hibernate.c3p0.acquire_increment">2</prop>
<!-- -->
<prop key="hibernate.c3p0.validate">true</prop>
</props>
</property>
</bean>
<!-- database template -->
<bean id="databaseHibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="databaseSessionFactory" />
</property>
</bean>
<bean id="databaseHibernateDao"
class="com.ibsrapp.database.persistent.dao.impl.IbsrappHibernateDaoImol">
<property name="hibernateTemplate" ref="databaseHibernateTemplate" />
<property name="template" ref="databaseHibernateTemplate" />
</bean>
<bean id="databaseJdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="databaseSource" />
</bean>
<bean id="databaseJdbcDao" class="com.ibsrapp.database.persistent.dao.impl.IbsrappJdbcDaoImpl">
<property name="jdbcTemplate" ref="databaseJdbcTemplate" />
<property name="template" ref="databaseJdbcTemplate"/>
</bean>
<bean id="databaseJDBCCommonDao" class="com.ibsrapp.database.persistent.dao.IbsrappJDBCCommonDao">
<property name="sessionFactory">
<ref bean="databaseSessionFactory" />
</property>
</bean>
</beans>
(2)関連クラスpublic class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSourceType();
}
/**
* (non-Javadoc)
* @see javax.sql.CommonDataSource#getParentLogger()
*/
public Logger getParentLogger() throws SQLFeatureNotSupportedException
{
// TODO Auto-generated method stub
return null;
}
}
public class DynamicDataSourceHolder
{
//
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
//
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
//
public static String getDataSourceType() {
return (String) contextHolder.get();
}
//
public static void clearDataSourceType() {
contextHolder.remove();
}
}
(3)関連コードの切り替えDynamicDataSourceHolder.setDataSourceType("APPSERVER1");
ここで使用するのは、springプロファイルに配置されたkeyの文字列です。