多数据源 同用sessionfactory空指针好还是用同一个sessionfactory空指针好

多数据源-SessionFactory - 软件架构设计当前位置:& &&&多数据源-SessionFactory多数据源-SessionFactory&&网友分享于:&&浏览:93次多数据源---SessionFactory
之前做过一个多database的例子,当时没有达到动态更换数据源的目的。
后来做了个多session工厂的例子,达到了动态更换数据源的目的了。在网上有些例子,
不过感觉有帮助但是不全,特此把我自己的弄得贴出来,如果说的不清楚的话,可以下载
附件看代码!
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName"&
&context:annotation-config /&
&bean id="parentDataSource"
class="mons.dbcp.BasicDataSource"&
&property name="driverClassName"&
&value&com.mysql.jdbc.Driver&/value&
&/property&
&property name="username" value="root"&&/property&
&property name="password" value="hejie"&&/property&
&bean id="primaryDataSource" parent="parentDataSource"&
&property name="url"&
&value&jdbc:mysql://localhost:3306/test1&/value&
&/property&
&bean id="backupDataSource" parent="parentDataSource"&
&property name="url"&
&value&jdbc:mysql://localhost:3306/test2&/value&
&/property&
&bean id="dataSource" scope="prototype"
class="com.mul.spring.MyAbstractRoutingDataSource"&
&property name="targetDataSources"&
&map key-type="java.lang.String"&
&entry key="admin1" value-ref="primaryDataSource" /&
&entry key="guest1" value-ref="backupDataSource" /&
&/property&
&property name="defaultTargetDataSource" ref="primaryDataSource" /&
&bean id="sessionFactory1"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&
&property name="dataSource"&
&ref bean="primaryDataSource" /&
&/property&
&property name="hibernateProperties"&
&prop key="hibernate.dialect"&
org.hibernate.dialect.MySQLDialect
&prop key="hibernate.show_sql"&true&/prop&
&prop key="hibernate.format_sql"&true&/prop&
&prop key="hibernate.hbm2ddl.auto"&update&/prop&
&prop key="hibernate.current_session_context_class"&
&prop key="hibernate.show_sql"&false&/prop&
&/property&
&property name="mappingResources"&
com/mul/entity/DataEntity.hbm.xml
&/property&
&property name="exposeTransactionAwareSessionFactory"&
&value&false&/value&
&/property&
&bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&
&property name="dataSource"&
&ref bean="backupDataSource" /&
&/property&
&property name="hibernateProperties"&
&prop key="hibernate.dialect"&
org.hibernate.dialect.MySQLDialect
&prop key="hibernate.show_sql"&true&/prop&
&prop key="hibernate.format_sql"&true&/prop&
&prop key="hibernate.hbm2ddl.auto"&update&/prop&
&prop key="hibernate.current_session_context_class"&
&prop key="hibernate.show_sql"&false&/prop&
&/property&
&property name="mappingResources"&
com/mul/entity/DataEntity.hbm.xml
&/property&
&property name="exposeTransactionAwareSessionFactory"&
&value&false&/value&
&/property&
&bean id="sessionFactory" class="com.mul.spring.MultiSessionFactory"&
&property name="sessionFactory"&&ref local="sessionFactory2"/&&/property&
&bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"&
&property name="sessionFactory" ref="sessionFactory" /&
&property name="hibernateManagedSession" value="false"/&--&
&tx:annotation-driven transaction-manager="transactionManager" /&
&context:component-scan base-package="com.mul" /&
&aop:config&
&aop:pointcut id="testservice"
expression="execution(* com.mul.service.*Service.*(..))" /&
&aop:advisor id="testServiceAd" advice-ref="txAdvice" pointcut-ref="testservice" /&
&/aop:config&
&tx:advice id="txAdvice" transaction-manager="transactionManager"&
&tx:attributes&
&tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/&
&tx:method name="*" propagation="REQUIRED"/&--&
&/tx:attributes&
&/tx:advice&
&!-- dao --&
&bean id="dataEntityDao" class="com.mul.dao.DataEntityDao"&
&!-- service --&
&bean id="dataEntityService" class="com.mul.service.DataEntityService"&
&?xml version="1.0" encoding="utf-8"?&
&!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&
Mapping file autogenerated by MyEclipse Persistence Tools
&hibernate-mapping&
&class name="com.mul.entity.DataEntity" table="dataentity"
mutable="false"&
&id name="id" type="java.lang.Integer"&
&column name="id" /&
&generator class="native"&&/generator&
&property name="name" type="java.lang.String"&
&column name="name" length="128" /&
&/property&
&property name="age"&
&column name="age" /&
&/property&
&/hibernate-mapping&
package com.mul.
import org.springframework.orm.hibernate3.support.HibernateDaoS
import com.mul.entity.DataE
public class DataEntityDao extends HibernateDaoSupport{
public DataEntity save(DataEntity de){
this.getSession().save(de);
package com.mul.
import com.mul.dao.DataEntityD
import com.mul.entity.DataE
import com.mul.spring.CustomerContextH
import com.mul.spring.DynamicDataSourceT
public class DataEntityService {
private DataEntityDao dataEntityD
public DataEntity save(DataEntity de){
System.out.println("保存到s1:"+DynamicDataSourceType.S1);
this.dataEntityDao.save(de);
public DataEntity save2(DataEntity de){
System.out.println("保存到s2:"+DynamicDataSourceType.S2);
this.dataEntityDao.save(de);
public DataEntityDao getDataEntityDao() {
return dataEntityD
public void setDataEntityDao(DataEntityDao dataEntityDao) {
this.dataEntityDao = dataEntityD
servlet: 建议使用struts试一试,在servlet中跟我在网上查的不同
package com.mul.
import java.io.IOE
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import com.mul.entity.DataE
import com.mul.service.DataEntityS
import com.mul.spring.CustomerContextH
import com.mul.spring.DataSourceTypeC
import com.mul.spring.DataSourceTypeK
import com.mul.spring.DynamicDataSourceT
public class mulDataServlet extends HttpServlet {
public mulDataServlet() {
public void destroy() {
super.destroy();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
String db = request.getParameter("db");
DataEntity de = new DataEntity(name,new Integer(age==null?"16":age));
System.out.println("要保存的对象:"+de);
if("admin1".equals(db))
DataSourceTypeChoose.setThreadLocal(DataSourceTypeKey.ADMIN);
DataSourceTypeChoose.setThreadLocal(DataSourceTypeKey.GUEST);
if(DataSourceTypeKey.appContext == null)
DataSourceTypeKey.appContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
DataEntityService dataEntityService = (DataEntityService)DataSourceTypeKey.appContext.getBean("dataEntityService");
System.out.println("开始保存到数据库");
if("admin1".equals(db)){
//必须在save方法
CustomerContextHolder.setCustomerType(DynamicDataSourceType.S1);
dataEntityService.save(de);
CustomerContextHolder.setCustomerType(DynamicDataSourceType.S2);
dataEntityService.save2(de);
ApplicationContext appContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
DataEntityService dataE = (DataEntityService)appContext.getBean("dataEntityService");
dataE.save(de);
request.setAttribute("de", de);
request.getRequestDispatcher("/index.jsp").forward(request, response);
* Initialization of the servlet. &br&
* @throws ServletException if an error occurs
public void init() throws ServletException {
// Put your code here
辅助类,为了线程安全
package com.mul.
public class CustomerContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setCustomerType(String customerType) {
System.out.println(customerType+"
customerType cannot be null");
contextHolder.set(customerType);
public static String getCustomerType() {
return (String) contextHolder.get();
public static void clearCustomerType() {
contextHolder.remove();
统一管理session工厂的id
package com.mul.
public class DynamicDataSourceType {
public static final String S1= "sessionFactory1";
public static final String S2= "sessionFactory2";
多sessionFactory的实现
package com.mul.
import java.io.S
import java.lang.ref.R
import java.sql.C
import java.util.M
import java.util.S
import javax.naming.NamingE
import mons.logging.L
import mons.logging.LogF
import org.hibernate.HibernateE
import org.hibernate.I
import org.hibernate.S
import org.hibernate.SessionF
import org.hibernate.StatelessS
import org.hibernate.engine.FilterD
import org.hibernate.metadata.ClassM
import org.hibernate.metadata.CollectionM
import org.hibernate.stat.S
import org.springframework.beans.factory.NoSuchBeanDefinitionE
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
* 多SessionFactory 的一个实现
* @author hejie
public class MultiSessionFactory implements SessionFactory, ApplicationContextAware {
private static final long serialVersionUID = 3496378L;
private static final Log log = LogFactory.getLog(MultiSessionFactory.class);
private ApplicationContext applicationContext =
private SessionFactory sessionFactory =
public ApplicationContext getApplicationContext() {
return applicationC
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationC
//在每次拿到bean时都会检查你当前的sessionFactory,sessionFactoryName就是bean的id
public SessionFactory getSessionFactory(String sessionFactoryName) {
System.out.println("=========sessionFactoryName:"+sessionFactoryName);
if(sessionFactoryName==null||sessionFactoryName.equals("")){
return sessionF
return (SessionFactory)this.getApplicationContext().getBean(sessionFactoryName);
}catch(NoSuchBeanDefinitionException ex){
throw new RuntimeException("There is not the sessionFactory &name:"+sessionFactoryName+"& in the applicationContext!");
public SessionFactory getSessionFactory() {
String sessionFactoryName = CustomerContextHolder.getCustomerType();
return getSessionFactory(sessionFactoryName);
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionF
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#close()
public void close() throws HibernateException {
getSessionFactory().close();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evict(java.lang.Class)
public void evict(Class persistentClass) throws HibernateException {
getSessionFactory().evict(persistentClass);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evict(java.lang.Class, java.io.Serializable)
public void evict(Class persistentClass, Serializable id) throws HibernateException {
getSessionFactory().evict(persistentClass, id);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictCollection(java.lang.String)
public void evictCollection(String roleName) throws HibernateException {
getSessionFactory().evictCollection(roleName);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictCollection(java.lang.String, java.io.Serializable)
public void evictCollection(String roleName, Serializable id) throws HibernateException {
getSessionFactory().evictCollection(roleName, id);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictEntity(java.lang.String)
public void evictEntity(String entityName) throws HibernateException {
getSessionFactory().evictEntity(entityName);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictEntity(java.lang.String, java.io.Serializable)
public void evictEntity(String entityName, Serializable id) throws HibernateException {
getSessionFactory().evictEntity(entityName, id);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictQueries()
public void evictQueries() throws HibernateException {
getSessionFactory().evictQueries();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#evictQueries(java.lang.String)
public void evictQueries(String cacheRegion) throws HibernateException {
getSessionFactory().evictQueries(cacheRegion);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getAllClassMetadata()
public Map getAllClassMetadata() throws HibernateException {
return getSessionFactory().getAllClassMetadata();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getAllCollectionMetadata()
public Map getAllCollectionMetadata() throws HibernateException {
return getSessionFactory().getAllCollectionMetadata();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getClassMetadata(java.lang.Class)
public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException {
return getSessionFactory().getClassMetadata(persistentClass);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getClassMetadata(java.lang.String)
public ClassMetadata getClassMetadata(String entityName) throws HibernateException {
return getSessionFactory().getClassMetadata(entityName);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getCollectionMetadata(java.lang.String)
public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException {
return getSessionFactory().getCollectionMetadata(roleName);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getCurrentSession()
public org.hibernate.classic.Session getCurrentSession() throws HibernateException {
return getSessionFactory().getCurrentSession();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getDefinedFilterNames()
public Set getDefinedFilterNames() {
return getSessionFactory().getDefinedFilterNames();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getFilterDefinition(java.lang.String)
public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {
return getSessionFactory().getFilterDefinition(filterName);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#getStatistics()
public Statistics getStatistics() {
return getSessionFactory().getStatistics();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#isClosed()
public boolean isClosed() {
return getSessionFactory().isClosed();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openSession()
public org.hibernate.classic.Session openSession() throws HibernateException {
return getSessionFactory().openSession();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openSession(java.sql.Connection)
public org.hibernate.classic.Session openSession(Connection connection) {
return getSessionFactory().openSession(connection);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openSession(org.hibernate.Interceptor)
public org.hibernate.classic.Session openSession(Interceptor interceptor) throws HibernateException {
return getSessionFactory().openSession(interceptor);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openSession(java.sql.Connection, org.hibernate.Interceptor)
public org.hibernate.classic.Session openSession(Connection connection, Interceptor interceptor) {
return getSessionFactory().openSession(connection, interceptor);
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openStatelessSession()
public StatelessSession openStatelessSession() {
return getSessionFactory().openStatelessSession();
/* (non-Javadoc)
* @see org.hibernate.SessionFactory#openStatelessSession(java.sql.Connection)
public StatelessSession openStatelessSession(Connection connection) {
return getSessionFactory().openStatelessSession(connection);
/* (non-Javadoc)
* @see javax.naming.Referenceable#getReference()
public javax.naming.Reference getReference() throws NamingException {
return getSessionFactory().getReference();
只创建一个application(struts等就不需要了)
package com.mul.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class DataSourceTypeKey {
public static ApplicationContext appC
在servlet调用service时他去调用MultiSessionFactory openSession()
注意:发布项目是去掉asm-2.2.3.jar包
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有同一个模块使用多个sessionfactory的解决方案_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
同一个模块使用多个sessionfactory的解决方案
上传于||暂无简介
阅读已结束,如果下载本文需要使用
想免费下载本文?
你可能喜欢hibernate中的sessionfactory是不是最好不要创建多个呢
[问题点数:40分,结帖人meiqianmeifang]
hibernate中的sessionfactory是不是最好不要创建多个呢
[问题点数:40分,结帖人meiqianmeifang]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。3085人阅读
适用范围:适合SSH架构访问多个数据库,数据库的类型和表结构不必相同,且没有跨库事务的情况(跨库事务最好用分布式事务处理)。
实现方式:我们可以在spring的配置文件中配置多个sessionFactory,如:&bean id="aDataSource"&& class="mons.dbcp.BasicDataSource"&& destroy-method="close"&&& &property name="driverClassName"&&&& &value&${adriver}&/value&&& &/property&&& &property name="url"&&&& &value&${aurl}&/value&&& &/property&&& &property name="username"&&&& &value&${ausername}&/value&&& &/property&&& &property name="password"&&&& &value&${apassword}&/value&&& &/property&&/bean&&bean id="bDataSource"&& class="mons.dbcp.BasicDataSource"&& destroy-method="close"&&& &property name="driverClassName"&&&& &value&${bdriver}&/value&&& &/property&&& &property name="url"&&&& &value&${burl}&/value&&& &/property&&& &property name="username"&&&& &value&${busername}&/value&&& &/property&&& &property name="password"&&&& &value&${bpassword}&/value&&& &/property&&/bean&&bean id="cDataSource"&& class="mons.dbcp.BasicDataSource"&& destroy-method="close"&&& &property name="driverClassName"&&&& &value&${cdriver}&/value&&& &/property&&& &property name="url"&&&& &value&${curl}&/value&&& &/property&&& &property name="username"&&&& &value&${cusername}&/value&&& &/property&&& &property name="password"&&&& &value&${cpassword}&/value&&& &/property&&/bean&
&!-- Hibernate SessionFactorys --&&bean id="aSessionFactory"&& class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&&& &property name="dataSource"&&&& &ref local="aDataSource" /&&& &/property&&& &property name="mappingResources"&&&& &list&&&&& &value&&&&&& .hbm.xml文件&&&& &/value&&&& &/list&&& &/property&&& &property name="hibernateProperties"&&&& &props&&&&& &prop key="hibernate.dialect"&&&&&& ${ahibernate.dialect}&&&& &/prop&&&&& &prop key="hibernate.show_sql"&true&/prop&&&&& &prop key="format_sql"&true&/prop&&&& &/props&&& &/property&&/bean&&bean id="bSessionFactory"&& class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&&& &property name="dataSource"&&&& &ref local="bDataSource" /&&& &/property&&& &property name="mappingResources"&&&& &list&&&&& &value&&&&&& .hbm.xml文件&&&& &/value&&&& &/list&&& &/property&&& &property name="hibernateProperties"&&&& &props&&&&& &prop key="hibernate.dialect"&&&&&& ${bhibernate.dialect}&&&& &/prop&&&&& &prop key="hibernate.show_sql"&true&/prop&&&&& &prop key="format_sql"&true&/prop&&&& &/props&&& &/property&&/bean&&bean id="cSessionFactory"&& class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&&& &property name="dataSource"&&&& &ref local="cDataSource" /&&& &/property&&& &property name="mappingResources"&&&& &list&&&&& &value&&&&&&& .hbm.xml文件&&&& &/value&&&& &/list&&& &/property&&& &property name="hibernateProperties"&&&& &props&&&&& &prop key="hibernate.dialect"&&&&&& ${chibernate.dialect}&&&& &/prop&&&&& &prop key="hibernate.show_sql"&true&/prop&&&&& &prop key="format_sql"&true&/prop&&&& &/props&&& &/property&&/bean&&bean id="sessionFactory" class="com.cintel.dcp.datasource.MultiSessionFactory"&&& &property name="sessionFactory"&&ref local="aSessionFactory"/&&/property&&/bean&注意:最后一个com.cintel.dcp.datasource.MultiSessionFactory要自己实现,它实现了SessionFactory接口和ApplicationContext接口,如下:package com.cintel.dcp.
import java.io.Simport java.sql.Cimport java.util.Mimport java.util.S
import javax.naming.NamingEimport javax.naming.R
import mons.logging.Limport mons.logging.LogFimport org.hibernate.HibernateEimport org.hibernate.Iimport org.hibernate.SessionFimport org.hibernate.StatelessSimport org.hibernate.classic.Simport org.hibernate.engine.FilterDimport org.hibernate.metadata.ClassMimport org.hibernate.metadata.CollectionMimport org.hibernate.stat.Simport org.springframework.beans.factory.NoSuchBeanDefinitionEimport org.springframework.context.ApplicationCimport org.springframework.context.ApplicationContextApublic class MultiSessionFactory implements SessionFactory, ApplicationContextAware {private static final long serialVersionUID = 3496378L;private static final Log log = LogFactory.getLog(MultiSessionFactory.class);private ApplicationContext applicationContext =private SessionFactory sessionFactory =public ApplicationContext getApplicationContext() {&& return applicationC}public void setApplicationContext(ApplicationContext applicationContext) {&& this.applicationContext = applicationC}public SessionFactory getSessionFactory(String sessionFactoryName) {&& log.debug("sessionFactoryName:"+sessionFactoryName);&& try{&&& if(sessionFactoryName==null||sessionFactoryName.equals("")){&&&& return sessionF&&& }&&& return (SessionFactory)this.getApplicationContext().getBean(sessionFactoryName);&& }catch(NoSuchBeanDefinitionException ex){&&& throw new RuntimeException("There is not the sessionFactory &name:"+sessionFactoryName+"& in the applicationContext!");&& }}public SessionFactory getSessionFactory() {&& String sessionFactoryName = CustomerContextHolder.getCustomerType();&& return getSessionFactory(sessionFactoryName);}public void setSessionFactory(SessionFactory sessionFactory) {&& this.sessionFactory = sessionF}/* (non-Javadoc)* @see org.hibernate.SessionFactory#close()*/public void close() throws HibernateException {&& getSessionFactory().close();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evict(java.lang.Class)*/public void evict(Class persistentClass) throws HibernateException {&& getSessionFactory().evict(persistentClass);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evict(java.lang.Class, java.io.Serializable)*/public void evict(Class persistentClass, Serializable id) throws HibernateException {&& getSessionFactory().evict(persistentClass, id);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictCollection(java.lang.String)*/public void evictCollection(String roleName) throws HibernateException {&& getSessionFactory().evictCollection(roleName);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictCollection(java.lang.String, java.io.Serializable)*/public void evictCollection(String roleName, Serializable id) throws HibernateException {&& getSessionFactory().evictCollection(roleName, id);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictEntity(java.lang.String)*/public void evictEntity(String entityName) throws HibernateException {&& getSessionFactory().evictEntity(entityName);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictEntity(java.lang.String, java.io.Serializable)*/public void evictEntity(String entityName, Serializable id) throws HibernateException {&& getSessionFactory().evictEntity(entityName, id);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictQueries()*/public void evictQueries() throws HibernateException {&& getSessionFactory().evictQueries();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#evictQueries(java.lang.String)*/public void evictQueries(String cacheRegion) throws HibernateException {&& getSessionFactory().evictQueries(cacheRegion);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getAllClassMetadata()*/public Map getAllClassMetadata() throws HibernateException {&& return getSessionFactory().getAllClassMetadata();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getAllCollectionMetadata()*/public Map getAllCollectionMetadata() throws HibernateException {&& return getSessionFactory().getAllCollectionMetadata();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getClassMetadata(java.lang.Class)*/public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException {&& return getSessionFactory().getClassMetadata(persistentClass);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getClassMetadata(java.lang.String)*/public ClassMetadata getClassMetadata(String entityName) throws HibernateException {&& return getSessionFactory().getClassMetadata(entityName);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getCollectionMetadata(java.lang.String)*/public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException {&& return getSessionFactory().getCollectionMetadata(roleName);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getCurrentSession()*/public Session getCurrentSession() throws HibernateException {&& return getSessionFactory().getCurrentSession();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getDefinedFilterNames()*/public Set getDefinedFilterNames() {&& return getSessionFactory().getDefinedFilterNames();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getFilterDefinition(java.lang.String)*/public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {&& return getSessionFactory().getFilterDefinition(filterName);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#getStatistics()*/public Statistics getStatistics() {&& return getSessionFactory().getStatistics();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#isClosed()*/public boolean isClosed() {&& return getSessionFactory().isClosed();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openSession()*/public Session openSession() throws HibernateException {&& return getSessionFactory().openSession();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openSession(java.sql.Connection)*/public Session openSession(Connection connection) {&& return getSessionFactory().openSession(connection);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openSession(org.hibernate.Interceptor)*/public Session openSession(Interceptor interceptor) throws HibernateException {&& return getSessionFactory().openSession(interceptor);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openSession(java.sql.Connection, org.hibernate.Interceptor)*/public Session openSession(Connection connection, Interceptor interceptor) {&& return getSessionFactory().openSession(connection, interceptor);}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openStatelessSession()*/public StatelessSession openStatelessSession() {&& return getSessionFactory().openStatelessSession();}/* (non-Javadoc)* @see org.hibernate.SessionFactory#openStatelessSession(java.sql.Connection)*/public StatelessSession openStatelessSession(Connection connection) {&& return getSessionFactory().openStatelessSession(connection);}/* (non-Javadoc)* @see javax.naming.Referenceable#getReference()*/public Reference getReference() throws NamingException {&& return getSessionFactory().getReference();}}
然后我用一个常量类来标识sessionFactorypublic class DynamicDataSourceType {public static final String A= "aSessionFactory";public static final String B= "bSessionFactory";public static final String C= "cSessionFactory";}
最后一个关键类:用来存放当前正在使用的sessionFactorypublic class CustomerContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setCustomerType(String customerType) {&& Assert.notNull(customerType, "customerType cannot be null");&& contextHolder.set(customerType);}
public static String getCustomerType() {&& return (String) contextHolder.get();}
public static void clearCustomerType() {&& contextHolder.remove();}}
可以在action、service、dao中进行数据库切换,切换方式:CustomerContextHolder.setCustomerType(DynamicDataSourceType.A);
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:27602次
排名:千里之外
原创:19篇
评论:12条
(1)(1)(2)(2)(3)(5)(5)

我要回帖

更多关于 获取sessionfactory 的文章

 

随机推荐