Spring Ibatis父子容器事务失效解决办法

在父子容器中都存在数据源的配置,父容器中采用默认名称“dataSource”作为id,子容器开发的使用者在配置数据源时并不知道父容器的配置,导致数据源的id一样,这时候就抛出了DataSource is Closed的错误消息,随后修改子容器的数据源名称为dataSourceWidgetpt,这个错误解决,但是此时在子容器中配置事务发现,事务失效。


父子容器的数据源配置图和事务失效原因:


下面是Spring的配置文件,红色注释要注意:

<?xml version="1.0" encoding="GBK"?>

<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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName">


<!-- 以后其他模块也需要DataSource的话,统一移到到common模块(需要新建的) -->
<bean id="dataSourceWidgetpt" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.alibaba.china.jdbc.SimpleDriver" />
<property name="url" value="${widgetpt_database_driver_url}" />
<property name="username" value="${widgetpt_database_driver_username}" />
<property name="password">
<bean
class="com.alibaba.china.galaxy.common.spring.MapChooseValueFactoryBean">
<property name="key" value="${widgetpt_production}" />
<property name="objects">
<map>
<entry key="run">
<bean
class="com.alibaba.china.biz.common.security.EncryptDBPasswordFactory">
<property name="password"
value="${widgetpt_database_driver_password_encrypted}" />
</bean>
</entry>
<entry key="dev" value="${widgetpt_database_driver_password}" />
<entry key="test" value="${widgetpt_database_driver_password}" />
</map>
</property>
</bean>
</property>
<property name="maxActive">
<value>14</value>
</property>
<property name="initialSize">
<value>1</value>
</property>
<property name="maxWait">
<value>60000</value>
</property>
<property name="maxIdle">
<value>14</value>
</property>
<property name="minIdle">
<value>1</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>60000</value>
</property>
<property name="minEvictableIdleTimeMillis">
<value>1800000</value>
</property>
<property name="connectionProperties">
<value>bigStringTryClob=true;clientEncoding=GBK;defaultRowPrefetch=50;serverEncoding=ISO-8859-1</value>
</property>
</bean>
<!-- spring 事务回滚需要用到的事务管理器。 -->
<bean name="transactionManagerWidgetpt"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceWidgetpt" />
</bean>


<bean id="transactionBaseWidgetpt"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManagerWidgetpt" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="upload*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="publish*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<!-- <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> 
<property name="transactionManager" ref="transactionManager" /> </bean> -->

</beans>

下面是DAO和Service的配置

<bean id="pluginConfigServicetarget" class="com.alibaba.widgetpt.plugin.PluginConfigServiceImpl" />


<import resource="classpath:spring/widgetpt-datasource-oracle.xml" />

<bean id="pluginConfigService" parent="transactionBaseWidgetpt">
<property name="target" ref="pluginConfigServicetarget" />   
</bean>

<!-- 默认的sqlMapClient,数据源为oracle -->
<bean id="sqlMapClientWidgetpt" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

<!--  这里也支持数据源配置,但是不能配置在sqlMapClient中需要配置在下面的sqlMapClientDAO中,原因可以看后面的解释图  -->

<property name="configLocation" value="classpath:sqlmap/widgetpt-default-sqlmap.xml" />
</bean>


<bean id="sqlMapClientDAO" abstract="true">

<!--  数据源一定要配置在这里,不能配置在上面的sqlMapClientWidgetpt中  -->

<property name="dataSource" ref="dataSourceWidgetpt" />
<property name="sqlMapClient" ref="sqlMapClientWidgetpt" />
</bean>

<bean id="pluginSummaryDAO" parent="sqlMapClientDAO"
class="com.alibaba.widgetpt.plugin.config.dao.support.IBatisPluginSummaryDAO" />

<bean id="pluginManageDAO" parent="sqlMapClientDAO"
class="com.alibaba.widgetpt.plugin.config.dao.support.IBatisPluginManageDAO" />

<bean id="pluginHistoryDAO" parent="sqlMapClientDAO"
class="com.alibaba.widgetpt.plugin.config.dao.support.IBatisPluginHistoryDAO" />

<bean id="pluginResourceDAO" parent="sqlMapClientDAO"
class="com.alibaba.widgetpt.plugin.config.dao.support.IBatisPluginResourceDAO" />

<bean id="pluginSiteInfoDAO" parent="sqlMapClientDAO"
class="com.alibaba.widgetpt.plugin.config.dao.support.IBatisPluginSiteInfoDAO" />


<import resource="classpath:spring/widgetpt-normandy-beans.xml" />
</beans>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章