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

关于ibatis+spring整合spring声明式事物不起作用的有关问题

2013-07-20 
关于ibatis+spring整合spring声明式事物不起作用的问题spring的配置文件:?xml version1.0 encodingU

关于ibatis+spring整合spring声明式事物不起作用的问题
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 读取jdbc配置文件中的数据库资源 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- properties文件为只读 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"></property>
<!-- 如果找不到该文件就忽略 -->
<property name="ignoreResourceNotFound" value="true"></property>
<property name="locations">
<list>
<!-- 数据库配置文件 -->
<value>classpath*:com/booklive/resources/config/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数据库连接数,设置为0时,表示没有限制 -->
<property name="maxActive" value="200"></property>
<!-- 最大等待连接中的数量,设置为0时,表示没有限制 -->
<property name="maxIdle" value="10"></property>
<!-- 最大等待秒数,单位为毫秒, 超过时间会报出错误信息 -->
<property name="maxWait" value="60"></property>
<property name="initialSize" value="5"></property>
<!-- 是否自我中断,默认是 false -->


<property name="removeAbandoned" value="false"></property>
</bean>

<!-- spring事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

 <!-- ibatis操作对象 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>/WEB-INF/SqlMapConfig.xml</value>
</property>
</bean>

<!-- sqlMapClientTemplate -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
 </bean>

<!-- dao数据访问层 -->
<bean id="ibaseDao" class="com.booklive.basic.BaseIbatisDaoImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
 
<!-- aop动态代理 事物拦截器-->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>              
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!-- 加入各模块spring配置文件 -->
<import resource="spring-login.xml"/>
<import resource="spring-menu.xml"/>


<import resource="spring-bookmanager.xml"/>
</beans>



BaseIbatisDaoImpl实现类:

public class BaseIbatisDaoImpl extends SqlMapClientDaoSupport implements BaseDao{

/**
 * 根据条件查询对象集合
 * @author coffee
 */

public <T> List<T> queryList(String sqlid,Object paramObj){
return (List<T>)this.getSqlMapClientTemplate().queryForList(sqlid, paramObj);
}

/**
 * 根据条件查询对象所有数据
 * @author coffee
 */
public <T> List<T> queryAllList(String sqlid){
return (List<T>)this.getSqlMapClientTemplate().queryForList(sqlid);
}

}




[解决办法]
是异常的问题
事物提交是根据异常来回滚的,默认是runtimeException 异常 而空异常不是不属于这个异常
解决 : 1.程序中捕捉空异常 然后Throw  runtimeException
       或者配置文件中 配置只要遇到exception 就作事物回滚

热点排行