首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

ssh多数据库配置 如何get session

2012-12-17 
ssh多数据库配置 怎么get session ?本帖最后由 tigerwithwing 于 2012-12-04 22:41:33 编辑struts2+spring

ssh多数据库配置 怎么get session ?
本帖最后由 tigerwithwing 于 2012-12-04 22:41:33 编辑 struts2+spring3.0+hibernate3.3
我配置的是双数据源,我想在程序中执行hql和sql语句,要执行hql和sql语句的前提是先getSession()。
怎么去获取2个数据源的 getSession()呢?请教高手。有代码例子最好。多谢多谢!
我的spring 配置文件applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
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">

<import resource="MpssServiceContext.xml" />
<!--数据源一: mpsss数据源   -->
<bean id="mpssHibernateSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:/com/hncatv/orm/hibernate/MpssHibernate.cfg.xml">
</property>
</bean>
<!--数据源二: worker数据源   -->
<bean id="workerHibernateSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:/com/hncatv/orm/hibernate/WorkerHibernate.cfg.xml">
</property>
</bean>


<bean id="mpssHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mpssHibernateSessionFactory"></property>
</bean>
<bean id="workerHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="workerHibernateSessionFactory"></property>
</bean>

<bean id="mpssTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mpssHibernateSessionFactory" />
</bean>
<bean id="workerTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="workerHibernateSessionFactory" />
</bean>

<tx:advice id="mpssTxAdvice" transaction-manager="mpssTxManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<tx:advice id="workerTxAdvice" transaction-manager="workerTxManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />


<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>


[最优解释]
public class HibernateUtil {
private static SessionFactory sf_original;
private static SessionFactory sf_now;
static{
sf_original=new Configuration().configure("/hibernate_original.cfg.xml").buildSessionFactory();
sf_now=new Configuration().configure("/hibernate_now.cfg.xml").buildSessionFactory();
}
/**
 * 根据不同的参数名判断调用哪个sessionFactory
 * @param name
 * @return
 */
public static Session getSessionByName(String name){
Session session=null;
if(name.equals("original")){
session=sf_original.openSession();
}
if(name.equals("now")){
session=sf_now.openSession();
}
return session;
}
/**
 * 根据参数决定关闭哪个sessionFactory
 * @param name
 */
public static void closeSessionFactoryByName(String name){
if(name.equals("original")){
sf_original.close();
}
if(name.equals("now")){
sf_now.close();
}
}
}



类似这样
[其他解释]
数据源是两个那session也要是两个了。你注入哪个sessionFactory就从哪个上getCurrentSession();
[其他解释]
mpssHibernateTemplate.getSession()
workerHibernateTemplate.getSession()
[其他解释]
写一个hibernateUNIT类,定义不同的配置文件,设置不同的方法,然后调用就行了
[其他解释]
什么情况?没有人知道?
[其他解释]
引用:
mpssHibernateTemplate.getSession()
workerHibernateTemplate.getSession()

+1
[其他解释]
写个接口,想用哪个SESSION就从接口中取

热点排行