问题描述
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"default-lazy-init="true"><!--====================================================加载属性文件===============================================--><beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:jdbc.properties</value></list></property></bean><!--配置数据源,用了dbcp数据库连接池--><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"><value>${jdbc.driver}</value></property><propertyname="url"><value>${jdbc.url}</value></property><propertyname="username"><value>${jdbc.username}</value></property><propertyname="password"><value>${jdbc.password}</value></property><propertyname="maxActive"><value>${maxActive}</value></property><propertyname="maxWait"><value>${maxWait}</value></property><propertyname="maxIdle"><value>${maxIdle}</value></property><propertyname="removeAbandoned"><value>${removeAbandoned}</value></property><propertyname="removeAbandonedTimeout"><value>${removeAbandonedTimeout}</value></property><propertyname="validationQuery"><value>${validationQuery}</value></property><!--propertyname="defaultAutoCommit"><value>false</value></property--></bean><!--配置Jdbc模板--><beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><propertyname="dataSource"ref="dataSource"/></bean><!--配置事务管理器p:dataSource-ref="dataSource"/>--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="add*"propagation="REQUIRED"/><tx:methodname="edit*"propagation="REQUIRED"/><tx:methodname="remove*"propagation="REQUIRED"/><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="batchUpdate"propagation="REQUIRED"/><tx:methodname="*"read-only="true"/></tx:attributes></tx:advice><!--通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务--><aop:configproxy-target-class="true"><aop:pointcutid="serviceMethods"expression="execution(*app.com.th.db.service.*.*(..))"/><aop:advisorpointcut-ref="serviceMethods"advice-ref="txAdvice"/></aop:config>
packageapp.com.th.db.service;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importapp.com.th.db.dao.supJdbcDAO;importnet.sf.json.JSON;importnet.sf.json.JSONSerializer;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Repository;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;@RepositorypublicclassSupServiceImplimplementsSupService{@AutowiredprivateSupJdbcDAOsupJdbcDAO;publicvoidsaveSup()throwsException{//情况1:defaultAutoCommit设置为trueStringsql1="insertintoATTACHINFO(ATTACHID,ATTACHNAME)values(1,'test1')";supJdbcDAO.execute(sql1);Stringsql2="insertintoATTACHINFO(ATTACHID,ATTACHNAME1)values(2,'test2')";//错误语句supJdbcDAO.execute(sql2);/**情况2:defaultAutoCommit设置为falseStringsql1="insertintoATTACHINFO(ATTACHID,ATTACHNAME)values(1,'test1')";supJdbcDAO.execute(sql1);Stringsql2="insertintoATTACHINFO(ATTACHID,ATTACHNAME)values(2,'test2')";supJdbcDAO.execute(sql2);*/}}
执行这个saveSup方法中,情况1是我把defaultAutoCommit设置为true后自动提交sql1是正确的,sql2是错误的,执行完在数据库中看到第一条数据插入进去了。怎么进行事务回滚。情况2是我把defaultAutoCommit设置为false关闭自动提交执行完后,数据没有插入进去情况1不进行事务回滚;情况2事务没提交<aop:pointcutid="serviceMethods"expression="execution(*app.com.th.db.service.*.*(..))"/>切入点没问题啊配置的tx和aop怎么没作用了?
解决方案
解决方案二:
用springmvc的话,看看这里:https://github.com/springside/springside4/wiki/Spring-MVC
解决方案三:
程序里抛出newRuntimeException();试试
解决方案四:
引用2楼lilei19lilei29的回复:
程序里抛出newRuntimeException();试试
还是一样
解决方案五:
不知道你配置的对不,我都是注解配置,加个@Transaction
解决方案六:
引用4楼lilei19lilei29的回复:
不知道你配置的对不,我都是注解配置,加个@Transaction
那这样的话需要在每个方法前加这个注解比较麻烦了
解决方案七:
是不是要了出现EXCEPTION,回滚的配置<propkey="add*">PROPAGATION_REQUIRED,-Exception</prop>