后端框架:
Spring 、Spring mvc 、mybatiscss
业务需求:
client先从服务端获取用户大量信息到client,编辑完毕以后统一Post至服务端,对于数据的改动要么全成功,要么全失败,因此需要使用事务支持。html
问题:
配置Spring声明式事务,运行中出现异常未回滚.从网上查询获得一開始是本身的配置出了问题,由于配置文件的载入顺序决定了容器的载入顺序致使Spring事务没有起做用。mysql
详情例如如下:android
由于採用的是SpringMVC、 MyBatis,故统一採用了标注来声明Service、Controller
由于server启动时的载入配置文件的顺序为web.xml—root-context.xml(Spring的配置文件)—servlet-context.xml(SpringMVC的配置文件)。由于root-context.xml配置文件里Controller会先进行扫描装配。但是此时service尚未进行事务加强处理,获得的将是原样的Service(没有通过事务增强处理,故而没有事务处理能力)。因此咱们必须在root-context.xml中不扫描Controllerios
上面的问题解决后仍是没有回滚,后来了解到,Spring 仅仅会在程序运行中出现unchecked(RuntimeException)的异常时才会触发回滚。由于是与client直接交互的Server因此要将每一个处理结果以 errorcode 错误码和msg 错误信息的形式反馈给client因此显式捕捉了所有的异常,并将信息以Json数据格式发送给client这才致使了出现异常时事务没有回滚。git
由于要给client最真实、准确的错误信息反馈又不得不捕捉可能发生的异常又陷入了沉思.固然,问题老是有解决的方式的,哪怕是绕着走。github
以后从查询资料获得,捕捉可以,但是捕捉以后主动抛出仍是会引起事务回滚的!(喜)而后就想到在主动 throw new RuntimeException(“反馈给client的信息”);将要反馈给client的详细错误信息包装到异常信息中,发生异常时在Controller层catch异常,将信息返回至client。
(mysql 表的engine为InnoDB–支持事务回滚,默以为MyISAM–效率高)
到此,问题解决。web
岗位: Java服务端spring
工做内容: 接收来自client的请求(android,androidtv,ios,pc ..),对client请求数据作合法性校验,并与其它服务端交互获取client所需数据。sql
<?xml version="1.0" encoding="UTF-8"?> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置注解扫描,扫描 Controller层不扫描Service层 --> <context:component-scan base-package="cn.com.XX"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="true" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> <!-- <setting name="logImpl" value="LOG4J"/> --> </settings> <!-- mybatis分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql" /> </plugin> </plugins> </configuration>
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!--载入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:system.properties" /> </bean> <!--配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"> </property> <property name="jdbcUrl" value="${数据库链接}"> </property> <property name="user" value="root"></property> <property name="password" value="root"></property> <!--链接池中保留的最大链接数。
Default: 15 --> <property name="maxPoolSize" value="15"></property> <!--链接池中保留的最小链接数。 --> <property name="minPoolSize" value="3"></property> <!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--最大空暇时间,20秒内未使用则链接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="20"></property> <!--当链接池中的链接耗尽的时候c3p0一次同一时候获取的链接数。Default: 3 --> <property name="acquireIncrement"> <value>5</value> </property> <!-- JDBC的标准參数,用以控制数据源内载入的PreparedStatements数量。
但由于预缓存的statements 属于单个connection而不是整个链接池。因此设置这个參数需要考虑到多方面的因素。 假设maxStatements与maxStatementsPerConnection均为0。则缓存被关闭。
Default: 0 --> <property name="maxStatements"> <value>0</value> </property> <!--每60秒检查所有链接池中的空暇链接。Default: 0 --> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <!--定义在从数据库获取新链接失败后反复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts"> <value>30</value> </property> <!-- 获取链接失败将会引发所有等待链接池来获取链接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取链接。假设设为true,那么在尝试 获取链接失败后该数据源将申明已断开并永久关闭。Default: false --> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <!-- 因性能消耗大请仅仅在需要的时候使用它。
假设设为true那么在每一个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提高链接測试的性能。
Default: false --> <property name="testConnectionOnCheckout"> <value>true</value> </property> </bean> <!-- 注解扫描,不扫描Controller层 --> <context:component-scan base-package="cn.com.xx"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:cn/com/xx/**/*.xml" /> </bean> <!-- yxt add --> <bean id="mapperScanneryxt" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.com.xx.mapper" /> </bean> <!--配置事务管理器 --> <!-- transaction manager, use DataSourceTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 切面 --> <aop:config> <aop:pointcut id="fooServiceMethods" expression="execution(* cn.com.xx.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" /> </aop:config> <!--通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="select*" read-only="true" /> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> </beans>
try { log.info("check phone is exist before .."); int count = tMailingMapper.insert(record); if (count > 0) { log.info("加入 " + accountid + " 的好友" + account.getAccountid() + " " + phoneVo.getRemark() + " 成功"); } else { log.info("加入 " + accountid + " 的好友" + account.getAccountid() + " " + phoneVo.getRemark() + " 失败"); } } catch (Exception e) { log.error("加入联系人出现了异常 " + e.getMessage()); resJson.put("errorcode", "20022"); resJson.put("msg", "同步信息异常,请稍后重试"); throw new RuntimeException(resJson.toString()); }
@RequestMapping("/update/userinfo") public String updateUserInfo(HttpServletRequest request, HttpServletResponse response) { log.info("update userInfo start .."); String resText=null; try { resText = userService.updateUserInfo(request); } catch (Exception e) { log.error("更新信息失败,事务已回滚...",e); resText=e.getMessage(); } <-- 将操做结果返回给client--> HttpsUtil.sendAppMessage(resText, response); return null; }
本人初入Java,小白一枚,有不到之处还请见谅。
欢迎大神指点!
2016-03-19 10:58:58
參考文章:1. http://sence-qi.iteye.com/blog/1328902/
2.http://blog.sina.com.cn/s/blog_89ca421401016bmg.html