spring配置oracle多个用户的datasource

24 篇文章 0 订阅
6 篇文章 0 订阅

一个oracle下的两个用户,App和Fc。程序对Fc只有select操作,不涉及事务。App为一般应用,要求有事务。

 

  1. <bean id="dataSourceApp"  
  2.         class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  3.         <property name="driverClass"  
  4.             value="oracle.jdbc.driver.OracleDriver">  
  5.         </property>  
  6.         <property name="jdbcUrl"  
  7.             value="jdbc:oracle:thin:@localhost:1521:oranb">  
  8.         </property>  
  9.         <property name="user" value="stmg"></property>  
  10.         <property name="password" value="stmg"></property>  
  11.         <property name="minPoolSize">  
  12.             <value>1</value>  
  13.         </property>  
  14.         <property name="maxPoolSize">  
  15.             <value>10</value>  
  16.         </property>  
  17.         <property name="initialPoolSize">  
  18.             <value>10</value>  
  19.         </property>  
  20.         <property name="maxIdleTime">  
  21.             <value>30</value>  
  22.         </property>  
  23.         <property name="acquireIncrement">  
  24.             <value>5</value>  
  25.         </property>  
  26.         <property name="maxStatements">  
  27.             <value>0</value>  
  28.         </property>  
  29.         <property name="idleConnectionTestPeriod">  
  30.             <value>60</value>  
  31.         </property>  
  32.         <property name="acquireRetryAttempts">  
  33.             <value>30</value>  
  34.         </property>  
  35.         <property name="breakAfterAcquireFailure">  
  36.             <value>true</value>  
  37.         </property>  
  38.         <property name="testConnectionOnCheckout">  
  39.             <value>false</value>  
  40.         </property>  
  41.     </bean>  
  42.   
  43.     <!-- C3P0 -->  
  44.     <bean id="dataSourceFc"  
  45.         class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  46.         <property name="driverClass"  
  47.             value="oracle.jdbc.driver.OracleDriver">  
  48.         </property>  
  49.         <property name="jdbcUrl"  
  50.             value="jdbc:oracle:thin:@localhost:1521:oranb">  
  51.         </property>  
  52.         <property name="user" value="etpdb"></property>  
  53.         <property name="password" value="etpu1234#"></property>  
  54.         <property name="minPoolSize">  
  55.             <value>1</value>  
  56.         </property>  
  57.         <property name="maxPoolSize">  
  58.             <value>10</value>  
  59.         </property>  
  60.         <property name="initialPoolSize">  
  61.             <value>10</value>  
  62.         </property>  
  63.         <property name="maxIdleTime">  
  64.             <value>30</value>  
  65.         </property>  
  66.         <property name="acquireIncrement">  
  67.             <value>5</value>  
  68.         </property>  
  69.         <property name="maxStatements">  
  70.             <value>0</value>  
  71.         </property>  
  72.         <property name="idleConnectionTestPeriod">  
  73.             <value>60</value>  
  74.         </property>  
  75.         <property name="acquireRetryAttempts">  
  76.             <value>30</value>  
  77.         </property>  
  78.         <property name="breakAfterAcquireFailure">  
  79.             <value>true</value>  
  80.         </property>  
  81.         <property name="testConnectionOnCheckout">  
  82.             <value>false</value>  
  83.         </property>  
  84.     </bean>   
  85.       
  86.     <!-- sessionFactory App -->  
  87.     <bean id="sessionFactory"  
  88.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  89.         <property name="dataSource">  
  90.             <ref bean="dataSourceApp" />  
  91.         </property>  
  92.         <property name="hibernateProperties">  
  93.             <props>  
  94.                 <prop key="hibernate.dialect">  
  95.                     org.hibernate.dialect.Oracle9Dialect  
  96.                 </prop>  
  97.                 <!-- 允许自动提交 -->  
  98.                 <prop key="hibernate.connection.autocommit">false</prop>  
  99.                 <!-- 显示sql语句 -->  
  100.                 <prop key="hibernate.show_sql">true</prop>  
  101.             </props>  
  102.         </property>  
  103.           
  104.         <property name="mappingResources">  
  105.             <list>  
  106.                 <value>com/mutildatasource/po/Iasolution.hbm.xml</value>  
  107.                 <value>com/mutildatasource/po/Iaquestion.hbm.xml</value>  
  108.             </list>  
  109.         </property>  
  110.     </bean>  
  111.     <!-- sessionFactory Fc -->  
  112.     <bean id="sessionFactoryFc"  
  113.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  114.         <property name="dataSource">  
  115.             <ref bean="dataSourceFc" />  
  116.         </property>  
  117.         <property name="hibernateProperties">  
  118.             <props>  
  119.                 <prop key="hibernate.dialect">  
  120.                     org.hibernate.dialect.Oracle9Dialect  
  121.                 </prop>  
  122.                 <!-- 允许自动提交 -->  
  123.                 <prop key="hibernate.connection.autocommit">false</prop>  
  124.                 <!-- 显示sql语句 -->  
  125.                 <prop key="hibernate.show_sql">true</prop>  
  126.             </props>  
  127.         </property>  
  128.     </bean>  
  129.   
  130.     <!-- App hibernateTemplate -->  
  131.     <bean id="hibernateTemplate"  
  132.         class="org.springframework.orm.hibernate3.HibernateTemplate">  
  133.         <property name="sessionFactory">  
  134.             <ref bean="sessionFactory" />  
  135.         </property>  
  136.     </bean>  
  137.     <!-- Fc hibernateTemplate -->  
  138.     <bean id="hibernateTemplateFc"  
  139.         class="org.springframework.orm.hibernate3.HibernateTemplate">  
  140.         <property name="sessionFactory">  
  141.             <ref bean="sessionFactoryFc" />  
  142.         </property>  
  143.     </bean>  
  144.       
  145.     <!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->  
  146.     <bean id="transactionManager"  
  147.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  148.         <!-- HibernateTransactionManager bean需要依赖注入一个SessionFactory bean的引用-->  
  149.         <property name="sessionFactory">  
  150.             <ref local="sessionFactory" />  
  151.         </property>  
  152.     </bean>  
  153.   
  154.     <!-- 配置事务拦截器-->  
  155.     <bean id="transactionInterceptor"  
  156.         class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  157.         <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->  
  158.         <property name="transactionManager" ref="transactionManager" />  
  159.         <property name="transactionAttributes">  
  160.             <!-- 下面定义事务传播属性-->  
  161.             <props>  
  162.                 <prop key="save*">PROPAGATION_REQUIRED</prop>  
  163.                 <prop key="set*">PROPAGATION_REQUIRED</prop>  
  164.                 <prop key="update*">PROPAGATION_REQUIRED</prop>  
  165.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  166.                 <prop key="sort*">PROPAGATION_REQUIRED</prop>  
  167.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  168.             </props>  
  169.         </property>  
  170.     </bean>  
  171.     <!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性      
  172.         这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理 -->  
  173.     <bean  
  174.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  175.         <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->  
  176.         <property name="beanNames">  
  177.             <!-- 下面是所有需要自动创建事务代理的bean-->  
  178.             <list>  
  179.                 <value>mutilDataSourceService</value>  
  180.             </list>  
  181.             <!-- 此处可增加其他需要自动创建事务代理的bean-->  
  182.             </property>  
  183.         <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->  
  184.           
  185.         <property name="interceptorNames">  
  186.             <list>  
  187.                 <value>transactionInterceptor</value>  
  188.                 <!-- 此处可增加其他新的Interceptor -->  
  189.             </list>  
  190.         </property>  
  191.     </bean>  

 

  1. <bean id="appDao" class="com.mutildatasource.dao.impl.AppDaoImpl">  
  2.         <property name="hibernateTemplate">  
  3.             <ref bean="hibernateTemplate"/>  
  4.         </property>  
  5.     </bean>  
  6.       
  7.     <bean id="fcDao" class="com.mutildatasource.dao.impl.FcDaoImpl">  
  8.         <property name="hibernateTemplateFc">  
  9.             <ref bean="hibernateTemplateFc"/>  
  10.         </property>  
  11.         <property name="hibernateTemplate">  
  12.             <ref bean="hibernateTemplate"/>  
  13.         </property>  
  14.     </bean>  
  15.       
  16.     <bean id="mutilDataSourceService" class="com.mutildatasource.service.impl.MutilDataSourceServiceImpl">  
  17.         <property name="appDao">  
  18.             <ref bean="appDao"/>  
  19.         </property>  
  20.         <property name="fcDao">  
  21.             <ref bean="fcDao"/>  
  22.         </property>  
  23.     </bean>  
  24.       
  25.     <bean name="/datasource"  
  26.         class="com.mutildatasource.struts.action.MutilDataSourceAction">  
  27.         <property name="mutilDataSourceService">  
  28.             <ref bean="mutilDataSourceService" />  
  29.         </property>  
  30.     </bean>  

 

 

service : updateAppData() 和 updateAppData2()之一发生异常,两个都会回滚,updateFcData()则不会,完全和以上两个无关(实际需求中只有select),也就是只有updateAppData,updateAppData2是在一个事务中的。

  1. public class MutilDataSourceServiceImpl implements MutilDataSourceService {  
  2.       
  3.     private AppDaoImpl appDao;  
  4.     private FcDaoImpl fcDao;  
  5.   
  6.     public String getMutilDataSourceDataService(int answerid, int status) {  
  7.         String result = fcDao.updateFcData();  
  8.         appDao.updateAppData(answerid, status);  
  9.         appDao.updateAppData2(answerid, status);  
  10.         return null;  
  11.     }  
  12.   
  13.     public void setAppDao(AppDaoImpl appDao) {  
  14.         this.appDao = appDao;  
  15.     }  
  16.   
  17.     public void setFcDao(FcDaoImpl fcDao) {  
  18.         this.fcDao = fcDao;  
  19.     }  
  20.   
  21. }  

 

dao

  1. public class FcDaoImpl extends HibernateDaoSupport implements FcDao {  
  2.       
  3.     private HibernateTemplate hibernateTemplateFc;  
  4.       
  5.     public String updateCaiHuiData() {  
  6.         final String sql = "update tbusinflag set c_type='1' where c_businflag='01'";  
  7.         return (String)hibernateTemplateFc.execute(new HibernateCallback() {  
  8.             public Object doInHibernate(Session session) throws HibernateException {  
  9.                 Query query = session.createSQLQuery(sql);  
  10.                 query.executeUpdate();  
  11.                 return null;  
  12.             }  
  13.         });  
  14.           
  15.     }  
  16.   
  17.     public void setHibernateTemplateFc(HibernateTemplate hibernateTemplateFc) {  
  18.         this.hibernateTemplateFc = hibernateTemplateFc;  
  19.     }  
  20.   
  21. }  

 

  1. public class AppDaoImpl extends HibernateDaoSupport implements AppDao{  
  2.   
  3.     public void updateAppData(final int answerid, final int status) {  
  4.         getHibernateTemplate().execute(new HibernateCallback() {  
  5.             public Object doInHibernate(Session session)  
  6.                     throws HibernateException {  
  7.                 String hql = "UPDATE Iaquestion SET stsatus=? WHERE answerid=?";    //异常  
  8.                 Query q = session.createQuery(hql);  
  9.                 q.setInteger(0888);  
  10.                 q.setInteger(129);  
  11.                 q.executeUpdate();  
  12.                 return null;  
  13.             }  
  14.         });  
  15.     }  
  16.   
  17.     public void updateAppData2(final int solutionid, final int status) {  
  18.         getHibernateTemplate().execute(new HibernateCallback() {  
  19.             public Object doInHibernate(Session session)  
  20.                     throws HibernateException {  
  21.                 String hql = "UPDATE Iasolution SET status=? WHERE solutionid=?";   //正常  
  22.   
  23.     //              String hql = "UPDATE Iasolution SET statuss=? WHERE solutionid=?"; 异常  
  24.                 Query q = session.createQuery(hql);  
  25.                 q.setInteger(0, status);  
  26.                 q.setInteger(1, solutionid);  
  27.                 q.executeUpdate();  
  28.                 return null;  
  29.             }  
  30.         });  
  31.     }  
  32. }  

 

 

 

配置了两个sessionFactory不知道会不会有问题,Fc换成sqlserver不知道行不行,还有datasource换成JNDI,试试。


转载地址:http://blog.csdn.net/digyso888/article/details/4402264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值