同时插入两条数据,第一条成功,第二条失败,数据不回滚,是哪里配置出错?好心人给看看呗。
Site接口类SiteService.java
Site接口实现类SiteServiceImpl.java
Site控制类SiteController.java
BaoDao接口类BaseDao.java
BasoDao实现类BaseDaoImpl.java
数据库基础类SpringDB.java
applicationContext.xml 配置文件
Site接口类SiteService.java
Site接口实现类SiteServiceImpl.java
Site控制类SiteController.java
BaoDao接口类BaseDao.java
BasoDao实现类BaseDaoImpl.java
数据库基础类SpringDB.java
applicationContext.xml 配置文件
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!--将@Controller的注解排除掉 --> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 读取配置文件 --> <util:properties id="settings" location="classpath:config/global.properties" /> <!-- 读取数据库配置文件 --> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:config/jdbc.properties</value> </list> </property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties" /> </bean> <mvc:annotation-driven /> <!-- 数据源 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="${driver}" /> <property name="driverUrl" value="${driverUrl}" /> <property name="user" value="${user}" /> <property name="password" value="${password}" /> <property name="alias" value="proxool.aidecenter" /> <property name="maximumActiveTime" value="300000" /> <property name="prototypeCount" value="0" /> <property name="maximumConnectionCount" value="${maximumConnectionCount}" /> <property name="minimumConnectionCount" value="${minimumConnectionCount}" /> <property name="simultaneousBuildThrottle" value="50" /> <property name="houseKeepingTestSql" value="select form CURRENT_DATE" /> </bean> <!-- JDBC模版 --> <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="com.common.database.SpringDB.setJdbcTemplate" /> <property name="arguments" ref="jdbc" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <!-- 事务模板 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref local="transactionManager" /> </property> </bean> <!-- 事务特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 读取数据方法,一般采用只读事务 --> <tx:method name="find*" read-only="true" /> <!--以下方法,如save,update,delete等对数据库进行写入操作的方法,进行回滚 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 事务代理 --> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.service..*.*(..))" /> <aop:pointcut id="daoMethod" expression="execution(* com.dao.impl.*.*(..))" /> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" order="1" /> <aop:advisor pointcut-ref="daoMethod" advice-ref="txAdvice" order="1" /> </aop:config> </beans>
dispatcher-servlet.xml 配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" default-autowire="byName"> <!-- 将@Service注解给去掉 --> <context:component-scan base-package="com" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="useSuffixPatternMatch" value="true" /> <property name="interceptors"> <list> <ref bean="sessionInterceptor"></ref> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <ref bean="stringHttpMessageConverter" /> </list> </property> </bean> <!-- 负责读写字符串格式的数据 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> </list> </property> </bean> <!-- 负责读写入json格式的数据 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 拦截器 --> <bean id="sessionInterceptor" class="com.interceptor.SessionInterceptor"></bean> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="" /> <property name="suffix" value=".jsp" /> </bean> <!-- 启用缓存注解功能,该注解一定要声明在Spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager" /> <bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" /> <!-- 资源文件加载 --> <mvc:resources mapping="/upload/**" location="/upload/" cache-period="31556926" /> <mvc:resources mapping="/libs/**" location="/libs/" cache-period="31556926" /> </beans>
包结构
解决方案
20
在一个service中插入两天记录,第一条成功,第二条失败,由于事务存在,第一条记录也不会成功提交,这就是事务。
20
刚好本人最近也遇到过这样的问题
1、SpringMVC结构,Controller中最好不要做业务的逻辑处理,全部的逻辑处理应该放在Service中,Controller中只是调用service方法并返回对应的视图;
2、采用AOP织入事物和采用@transaction 的方式都可以开启事务,但是你在xml中已经配置了service包的方法开启事务,就不用再方法上面采用注解的方式了;
3、Spring的transaction默认要抛出异常才会回滚,所以在你采用事物的方法内不要使用try-catch或在catch里面抛出异常,本人是在Controller里面捕获异常的;
4、事物一般配置在service层上(你要把业务的逻辑代码全部放在service里面而不是Controller里面)。
个人的一点简介,也是最近的所学的,不对的地方请各位指正。
1、SpringMVC结构,Controller中最好不要做业务的逻辑处理,全部的逻辑处理应该放在Service中,Controller中只是调用service方法并返回对应的视图;
2、采用AOP织入事物和采用@transaction 的方式都可以开启事务,但是你在xml中已经配置了service包的方法开启事务,就不用再方法上面采用注解的方式了;
3、Spring的transaction默认要抛出异常才会回滚,所以在你采用事物的方法内不要使用try-catch或在catch里面抛出异常,本人是在Controller里面捕获异常的;
4、事物一般配置在service层上(你要把业务的逻辑代码全部放在service里面而不是Controller里面)。
个人的一点简介,也是最近的所学的,不对的地方请各位指正。