spring配置oracle多个用户的datasource

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

<bean id="dataSourceApp" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:oranb"> </property> <property name="user" value="stmg"></property> <property name="password" value="stmg"></property> <property name="minPoolSize"> <value>1</value> </property> <property name="maxPoolSize"> <value>10</value> </property> <property name="initialPoolSize"> <value>10</value> </property> <property name="maxIdleTime"> <value>30</value> </property> <property name="acquireIncrement"> <value>5</value> </property> <property name="maxStatements"> <value>0</value> </property> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <property name="acquireRetryAttempts"> <value>30</value> </property> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <!-- C3P0 --> <bean id="dataSourceFc" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:oranb"> </property> <property name="user" value="etpdb"></property> <property name="password" value="etpu1234#"></property> <property name="minPoolSize"> <value>1</value> </property> <property name="maxPoolSize"> <value>10</value> </property> <property name="initialPoolSize"> <value>10</value> </property> <property name="maxIdleTime"> <value>30</value> </property> <property name="acquireIncrement"> <value>5</value> </property> <property name="maxStatements"> <value>0</value> </property> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <property name="acquireRetryAttempts"> <value>30</value> </property> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <!-- sessionFactory App --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSourceApp" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <!-- 允许自动提交 --> <prop key="hibernate.connection.autocommit">false</prop> <!-- 显示sql语句 --> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/mutildatasource/po/Iasolution.hbm.xml</value> <value>com/mutildatasource/po/Iaquestion.hbm.xml</value> </list> </property> </bean> <!-- sessionFactory Fc --> <bean id="sessionFactoryFc" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSourceFc" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <!-- 允许自动提交 --> <prop key="hibernate.connection.autocommit">false</prop> <!-- 显示sql语句 --> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- App hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- Fc hibernateTemplate --> <bean id="hibernateTemplateFc" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactoryFc" /> </property> </bean> <!-- 定义事务管理器,使用适用于Hibernte的事务管理器--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- HibernateTransactionManager bean需要依赖注入一个SessionFactory bean的引用--> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!-- 配置事务拦截器--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事务拦截器bean需要依赖注入一个事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <!-- 下面定义事务传播属性--> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="set*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="sort*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性 这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定对满足哪些bean name的bean自动生成业务代理 --> <property name="beanNames"> <!-- 下面是所有需要自动创建事务代理的bean--> <list> <value>mutilDataSourceService</value> </list> <!-- 此处可增加其他需要自动创建事务代理的bean--> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> <!-- 此处可增加其他新的Interceptor --> </list> </property> </bean>

<bean id="appDao" class="com.mutildatasource.dao.impl.AppDaoImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <bean id="fcDao" class="com.mutildatasource.dao.impl.FcDaoImpl"> <property name="hibernateTemplateFc"> <ref bean="hibernateTemplateFc"/> </property> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <bean id="mutilDataSourceService" class="com.mutildatasource.service.impl.MutilDataSourceServiceImpl"> <property name="appDao"> <ref bean="appDao"/> </property> <property name="fcDao"> <ref bean="fcDao"/> </property> </bean> <bean name="/datasource" class="com.mutildatasource.struts.action.MutilDataSourceAction"> <property name="mutilDataSourceService"> <ref bean="mutilDataSourceService" /> </property> </bean>

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

public class MutilDataSourceServiceImpl implements MutilDataSourceService { private AppDaoImpl appDao; private FcDaoImpl fcDao; public String getMutilDataSourceDataService(int answerid, int status) { String result = fcDao.updateFcData(); appDao.updateAppData(answerid, status); appDao.updateAppData2(answerid, status); return null; } public void setAppDao(AppDaoImpl appDao) { this.appDao = appDao; } public void setFcDao(FcDaoImpl fcDao) { this.fcDao = fcDao; } }

dao

public class FcDaoImpl extends HibernateDaoSupport implements FcDao { private HibernateTemplate hibernateTemplateFc; public String updateCaiHuiData() { final String sql = "update tbusinflag set c_type='1' where c_businflag='01'"; return (String)hibernateTemplateFc.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createSQLQuery(sql); query.executeUpdate(); return null; } }); } public void setHibernateTemplateFc(HibernateTemplate hibernateTemplateFc) { this.hibernateTemplateFc = hibernateTemplateFc; } }

public class AppDaoImpl extends HibernateDaoSupport implements AppDao{ public void updateAppData(final int answerid, final int status) { getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { String hql = "UPDATE Iaquestion SET stsatus=? WHERE answerid=?"; //异常 Query q = session.createQuery(hql); q.setInteger(0, 888); q.setInteger(1, 29); q.executeUpdate(); return null; } }); } public void updateAppData2(final int solutionid, final int status) { getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { String hql = "UPDATE Iasolution SET status=? WHERE solutionid=?"; //正常 // String hql = "UPDATE Iasolution SET statuss=? WHERE solutionid=?"; 异常 Query q = session.createQuery(hql); q.setInteger(0, status); q.setInteger(1, solutionid); q.executeUpdate(); return null; } }); } }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,spring.datasource.dynamic.datasource.oracle是用于配置动态数据源的注解参数。在这种情况下,oracle是指定的数据源名称。动态数据源是使用dynamic-datasource框架来实现的,该框架允许在应用程序中动态切换数据源。通常建议在主从模式下遵循一般的规则,主数据库执行INSERT、UPDATE和DELETE操作,从数据库执行SELECT操作。使用dynamic-datasource-spring-boot-starter库可以轻松地引入动态数据源功能到Spring Boot项目中。可以在项目的配置文件(如application.yml)中配置数据源相关信息。具体的配置和使用方法可以参考引用。另外,引用提供了一个参考链接,可以解决可能遇到的问题。 [1][2<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springBoot(六) 调用多数据源 dynamic-datasource](https://blog.csdn.net/Tang_5253/article/details/101095855)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}](https://blog.csdn.net/xcl119xcl/article/details/89920606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值