Spring 事务管理详解

Spring 事务管理详解及使用 Spring 事务管理接口 1、事务管理器接口 PlatformTransactionManager 2、事务定义接口 TransactionDefinition Spring 事务管理的实现方法 1、没有事务管理的情况分析 2、通过配置 XML 实现事务管理 3、利用注解实现事务管理 4、在业务层实现事务管理 阅读详情

相信大家面试都经常会被问到框架相关的,或者有没有用过事务?那么看篇文章就对了。

1.什么是事务

       所谓事务是用户定义的一个数据库操作序列,这些操作要么全做,要么全不做,是一个不可分割的工作单位。

说白了就是要么一起成功,要么一起失败。不存在一些成功一些失败。充分说明了事务的原子性。

举个现实中最通俗易懂的栗子


以上就是我们经常会通过银行的转账系统来转账。如果没有事务,别人本来需要转账给你1000的 结果别人的账户里扣了1000 但是你的账户还是之前的0,你肯定会不开森不会同意的吧。事务的重要性就体现出来了,银行既要保证别人的利益又要保证你的利益。所以理解和使用事务还是非常重要的。尤其是金融行业。

2.事务的特性(简称ACID):

原子性 (Atomicity) :要么全做,要么全不做。

一致性 (Consistency) :说的是全部做,和全不做,这时数据库处于一致性,如果一个做,一个不做,就认为不一致。

隔离性(Isolation) :一个事务的执行不能被其他事务干扰,即一个事务的内部操作以及使用的数据对其他并发事务是隔离的。

持续性 (Durability) :一个事务一旦提交,它对数据库中数据的改变就应该是永久行的。、

 

3.事务并发操带来哪些问题

  3.1丢失修改

         两个事务T1和T2读入同一个数据并修改,T2提交的结果破坏了T1提交的结果,导致T1的修改被丢失。

  3.2不可重复读

         事务T1读取数据后,事务T2执行更新操作,使得T1无法再现前一次读到的结果。

  3.3读脏数据

        T1修改某一个数据并将其写回磁盘,事务T2读到T1修改之后的数据,这时T1由于某种原因被撤销,数据恢复到原来的值,T1读到的数据为脏数据。

  3.4幻读

       事务T1读取数据后,事务T2执行插入操作,T1再次读取,使T1出现了幻觉。

4.事务的API介绍

   4.1 事务管理器PlatformTransactionManager


spring为不同的持久化框架提供了不同的PlatformTransactionManager


   4.2 TransactionDefinition事务定义信息(隔离,传播,超时,只读)

       4.2.1 隔离级别

设置隔离级别可防止并发导致的问题。


如果选择DEFAULT则是数据库默认的隔离级别。

MySQL 默认REPEATABLE_READ

Oracle默认 READ_COMMITTED

  4.2.2 事务的传播行为


  4.2.3 TransactionStatus事务具体运作状态

void flush();冲洗数据库底层会话

boolean hasSavePoint();返回该事务是否有一个保存点

boolean isCompleted();返回事务是否提交或者回滚

boolean isNewTransaction();返回是否是一个新事务

boolean isRollbackOnly();判断这个事务是否已经设置了rollback-only。

void setRollbackOnly();设置这个事务rollback-only。

4.2.4 SavepointManager
 
事务回滚点管理接口,提供创建、释放回滚点,或者回滚到指定的回滚点。
 
 方法摘要:
 Object createSavepoint()
          Create a new savepoint.
          创建一个新的回滚点。
 void releaseSavepoint(Object savepoint)
          Explicitly release the given savepoint.
          释放一个给定的回滚点。
 void rollbackToSavepoint(Object savepoint)
          Roll back to the given savepoint.
          回滚到给定的回滚点。

5.转账示例

5.1 编程式事务管理

   5.1.1 创建数据库


5.1.2 Dao层

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface AccountDao {  
  2.   
  3.   
  4. /** 
  5. * @param out :转出账号 
  6. * @param money:转账金额 
  7. */  
  8. public void outMoney(String out,Double money);  
  9.   
  10. /** 
  11. *  
  12. * @param in :转入账号 
  13. * @param money:转账金额 
  14. */  
  15. public void inMoney(String in,Double money);  
  16. }  
  17.   
  18.   
  19. public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {  
  20.   
  21.   
  22. /** 
  23. * @param out :转出账号 
  24. * @param money:转账金额 
  25. */  
  26. @Override  
  27. public void outMoney(String out, Double money) {  
  28. String sql = "update account set money = money-? where name = ?";  
  29. this.getJdbcTemplate().update(sql, money, out);  
  30. }  
  31.   
  32.   
  33. /** 
  34. * @param in :转入账号 
  35. * @param money:转账金额 
  36. */  
  37. @Override  
  38. public void inMoney(String in, Double money) {  
  39. String sql = "update account set money = money+? where name = ?";  
  40. this.getJdbcTemplate().update(sql,money,in);  
  41. }  
  42.   
  43.   
  44. }  


5.1.3 Service层

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface AccountService {  
  2.   
  3.   
  4. /** 
  5. * @param out :转出账号 
  6. * @param in :转入账号 
  7. * @param money:转账金额 
  8. */  
  9. public void transfer(String out,String in,Double money);  
  10. }  
  11.   
  12.   
  13. public class AccountServiceImpl implements AccountService {  
  14.   
  15. //注入转账的DAO  
  16. private AccountDao accountDao;  
  17.   
  18.   
  19. //注入事务管理的模板  
  20. private TransactionTemplate transactionTemplate;  
  21.   
  22.   
  23. /** 
  24. * @param out :转出账号 
  25. * @param in :转入账号 
  26. * @param money:转账金额 
  27. */  
  28. @Override  
  29. public void transfer(final String out, final String in, final Double money) {  
  30. /*accountDao.outMoney(out, money); 
  31. //int i = 1/0; 
  32. accountDao.inMoney(in, money);*/  
  33.   
  34. //  
  35. transactionTemplate.execute(new TransactionCallbackWithoutResult() {  
  36.   
  37.   
  38. @Override  
  39. protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {  
  40. accountDao.outMoney(out, money);  
  41. //int i = 1/0;  
  42. accountDao.inMoney(in, money);  
  43. }  
  44. });  
  45. }  
  46.   
  47.   
  48. public void setAccountDao(AccountDao accountDao) {  
  49. this.accountDao = accountDao;  
  50. }  
  51.   
  52.   
  53. public void setTransactionTemplate(TransactionTemplate transactionTemplate) {  
  54. this.transactionTemplate = transactionTemplate;  
  55. }  
  56.   
  57.   
  58. }  


5.1.4 xml配置

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xmlns:aop="http://www.springframework.org/schema/aop"  
  6.      xmlns:tx="http://www.springframework.org/schema/tx"  
  7.      xmlns:task="http://www.springframework.org/schema/task"  
  8.      xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  16.          http://www.springframework.org/schema/task  
  17. http://www.springframework.org/schema/task/spring-task-3.1.xsd">  
  18.   
  19.   
  20. <!-- 引入外部的属性文件 -->  
  21. <context:property-placeholder location="classpath:jdbc.properties"/>  
  22.   
  23. <!-- 配置c3p0连接池 -->  
  24. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  25. <property name="driverClass" value="${jdbc.driverClass}" />  
  26. <property name="jdbcUrl" value="${jdbc.url}" />  
  27. <property name="user" value="${jdbc.username}" />  
  28. <property name="password" value="${jdbc.password}" />  
  29. </bean>  
  30.   
  31. <!-- 配置业务层类 -->  
  32. <bean id="accountService" class="com.zs.spring.demo1.AccountServiceImpl">  
  33. <property name="accountDao" ref="accountDao" />  
  34. <!-- 注入事务管理的模板 -->  
  35. <property name="transactionTemplate" ref="transactionTemplate" />  
  36. </bean>  
  37.   
  38. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->  
  39. <bean id="accountDao" class="com.zs.spring.demo1.AccountDaoImpl">  
  40. <property name="dataSource" ref="dataSource" />  
  41. </bean>  
  42.   
  43. <!-- 配置DAO类(未简化) -->  
  44. <!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  45. <property name="dataSource" ref="dataSource" />  
  46. </bean>  
  47. <bean id="accountDao" class="com.zs.spring.demo1.AccountDaoImpl">  
  48. <property name="jdbcTemplate" ref="jdbcTemplate" />  
  49. </bean> -->  
  50.   
  51. <!-- ==================================1.编程式的事务管理=============================================== -->  
  52. <!-- 配置事务管理器 -->  
  53. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  54. <property name="dataSource" ref="dataSource" />  
  55. </bean>  
  56.   
  57. <!-- 配置事务管理的模板:Spring为了简化事务管理的代码而提供的类 -->  
  58. <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
  59. <property name="transactionManager" ref="transactionManager"/>  
  60. </bean>  
  61.   
  62. </beans>  


5.1.5 测试类

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration("classpath:applicationContext1.xml")  
  3. public class TransactionTest {  
  4.   
  5.   
  6. @Resource(name="accountService")  
  7. private AccountService accountService;  
  8.   
  9. @Test  
  10. public void demo1(){  
  11. accountService.transfer("aaa", "bbb", 200d);  
  12. }  
  13. }  



5.2 使用xml配置申明式事务管理

代码侵入性小,是通过spring aop 实现的。

   5.2.1 使用XML配置声明式的事务管理(原始方式)

Service层的变化

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  public class AccountServiceImpl implements AccountService {  
  2.   
  3. //注入转账的DAO  
  4. private AccountDao accountDao;  
  5. public void setAccountDao(AccountDao accountDao) {  
  6. this.accountDao = accountDao;  
  7. }  
  8.   
  9.   
  10. /** 
  11. * @param out :转出账号 
  12. * @param in :转入账号 
  13. * @param money:转账金额 
  14. */  
  15. @Override  
  16. public void transfer( String out, String in, Double money) {  
  17. accountDao.outMoney(out, money);  
  18. //int i = 1/0;  
  19. accountDao.inMoney(in, money);  
  20.   
  21. }  
  22.   
  23. }  

xml的变化

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xmlns:aop="http://www.springframework.org/schema/aop"  
  6.      xmlns:tx="http://www.springframework.org/schema/tx"  
  7.      xmlns:task="http://www.springframework.org/schema/task"  
  8.      xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  16.          http://www.springframework.org/schema/task  
  17. http://www.springframework.org/schema/task/spring-task-3.1.xsd">  
  18.   
  19.   
  20. <!-- 引入外部的属性文件 -->  
  21. <context:property-placeholder location="classpath:jdbc.properties"/>  
  22.   
  23. <!-- 配置c3p0连接池 -->  
  24. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  25. <property name="driverClass" value="${jdbc.driverClass}" />  
  26. <property name="jdbcUrl" value="${jdbc.url}" />  
  27. <property name="user" value="${jdbc.username}" />  
  28. <property name="password" value="${jdbc.password}" />  
  29. </bean>  
  30.   
  31. <!-- 配置业务层类 -->  
  32. <bean id="accountService" class="com.zs.spring.demo2.AccountServiceImpl">  
  33. <property name="accountDao" ref="accountDao" />  
  34. </bean>  
  35.   
  36. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->  
  37. <bean id="accountDao" class="com.zs.spring.demo2.AccountDaoImpl">  
  38. <property name="dataSource" ref="dataSource" />  
  39. </bean>  
  40.   
  41. <!-- ==================================2.使用XML配置声明式的事务管理(原始方式)=============================================== -->  
  42.   
  43. <!-- 配置事务管理器 -->  
  44. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  45. <property name="dataSource" ref="dataSource" />  
  46. </bean>  
  47.   
  48. <!-- 配置业务层的代理 -->  
  49. <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  50. <!-- 配置目标对象 -->  
  51. <property name="target" ref="accountService" />  
  52. <!-- 注入事务管理器 -->  
  53. <property name="transactionManager" ref="transactionManager"></property>  
  54. <!-- 注入事务的属性 -->  
  55. <property name="transactionAttributes">  
  56. <props>  
  57. <!--   
  58. prop的格式:  
  59. * PROPAGATION :事务的传播行为  
  60. * ISOTATION :事务的隔离级别  
  61. * readOnly :只读  
  62. * -EXCEPTION :发生哪些异常回滚事务  
  63. * +EXCEPTION :发生哪些异常不回滚事务  
  64. -->  
  65. <prop key="transfer">PROPAGATION_REQUIRED</prop>  
  66. <!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> -->  
  67. <!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> -->  
  68. </props>  
  69. </property>  
  70. </bean>  
  71.   
  72.   
  73. </beans>  


 test类的变化

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration("classpath:applicationContext2.xml")  
  3. public class TransactionTest {  
  4.   
  5.   
  6. /** 
  7. * 一定要注入代理类:因为代理类进行增强的操作 
  8. */  
  9. //@Resource(name="accountService")  
  10. @Resource(name="accountServiceProxy")  
  11. private AccountService accountService;  
  12.   
  13. @Test  
  14. public void demo1(){  
  15. accountService.transfer("aaa", "bbb", 200d);  
  16. }  
  17. }  



5.3 使用XML配置声明式的事务管理,基于tx/aop

  5.3.1 xml配置

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xmlns:aop="http://www.springframework.org/schema/aop"  
  6.      xmlns:tx="http://www.springframework.org/schema/tx"  
  7.      xmlns:task="http://www.springframework.org/schema/task"  
  8.      xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  16.          http://www.springframework.org/schema/task  
  17. http://www.springframework.org/schema/task/spring-task-3.1.xsd">  
  18.   
  19.   
  20. <!-- 引入外部的属性文件 -->  
  21. <context:property-placeholder location="classpath:jdbc.properties"/>  
  22.   
  23. <!-- 配置c3p0连接池 -->  
  24. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  25. <property name="driverClass" value="${jdbc.driverClass}" />  
  26. <property name="jdbcUrl" value="${jdbc.url}" />  
  27. <property name="user" value="${jdbc.username}" />  
  28. <property name="password" value="${jdbc.password}" />  
  29. </bean>  
  30.   
  31. <!-- 配置业务层类 -->  
  32. <bean id="accountService" class="com.zs.spring.demo3.AccountServiceImpl">  
  33. <property name="accountDao" ref="accountDao" />  
  34. </bean>  
  35.   
  36. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->  
  37. <bean id="accountDao" class="com.zs.spring.demo3.AccountDaoImpl">  
  38. <property name="dataSource" ref="dataSource" />  
  39. </bean>  
  40.   
  41. <!-- ==================================3.使用XML配置声明式的事务管理,基于tx/aop=============================================== -->  
  42.   
  43. <!-- 配置事务管理器 -->  
  44. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  45. <property name="dataSource" ref="dataSource" />  
  46. </bean>  
  47.   
  48. <!-- 配置事务的通知 -->  
  49. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  50. <tx:attributes>  
  51. <!--   
  52. propagation :事务传播行为  
  53. isolation :事务的隔离级别  
  54. read-only :只读  
  55. rollback-for:发生哪些异常回滚  
  56. no-rollback-for:发生哪些异常不回滚  
  57. timeout :过期信息  
  58. -->  
  59. <tx:method name="transfer" propagation="REQUIRED"/>  
  60. </tx:attributes>  
  61. </tx:advice>  
  62.   
  63. <!-- 配置切面 -->  
  64. <aop:config>  
  65. <!-- 配置切入点 -->  
  66. <aop:pointcut expression="execution(* com.zs.spring.demo3.AccountService+.*(..))" id="pointcut1"/>  
  67. <!-- 配置切面 -->  
  68. <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>  
  69. </aop:config>  
  70.   
  71.   
  72. </beans>  


5.3.2 test的变化

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration("classpath:applicationContext3.xml")  
  3. public class TransactionTest {  
  4.   
  5.   
  6.   
  7. @Resource(name="accountService")  
  8. private AccountService accountService;  
  9.   
  10. @Test  
  11. public void demo1(){  
  12. accountService.transfer("aaa", "bbb", 200d);  
  13. }  
  14. }  


5.4 事务注解的实现

    5.4.1 xml配置

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xmlns:aop="http://www.springframework.org/schema/aop"  
  6.      xmlns:tx="http://www.springframework.org/schema/tx"  
  7.      xmlns:task="http://www.springframework.org/schema/task"  
  8.      xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  16.          http://www.springframework.org/schema/task  
  17. http://www.springframework.org/schema/task/spring-task-3.1.xsd">  
  18.   
  19.   
  20. <!-- 引入外部的属性文件 -->  
  21. <context:property-placeholder location="classpath:jdbc.properties"/>  
  22.   
  23. <!-- 配置c3p0连接池 -->  
  24. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  25. <property name="driverClass" value="${jdbc.driverClass}" />  
  26. <property name="jdbcUrl" value="${jdbc.url}" />  
  27. <property name="user" value="${jdbc.username}" />  
  28. <property name="password" value="${jdbc.password}" />  
  29. </bean>  
  30.   
  31. <!-- 配置业务层类 -->  
  32. <bean id="accountService" class="com.zs.spring.demo4.AccountServiceImpl">  
  33. <property name="accountDao" ref="accountDao" />  
  34. </bean>  
  35.   
  36. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->  
  37. <bean id="accountDao" class="com.zs.spring.demo4.AccountDaoImpl">  
  38. <property name="dataSource" ref="dataSource" />  
  39. </bean>  
  40.   
  41. <!-- ==================================4.使用注解配置声明式事务============================================ -->  
  42.   
  43.   
  44. <!-- 配置事务管理器 -->  
  45. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  46. <property name="dataSource" ref="dataSource" />  
  47. </bean>  
  48.   
  49. <!-- 开启注解事务 -->  
  50. <tx:annotation-driven transaction-manager="transactionManager"/>  
  51.   
  52. </beans>  


5.4.2 Serverce

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  *@Transactional(Propagation.REQUIRED)中的的属性  
  2.  *propagation :事务的传播行为  
  3.  *isolation :事务的隔离级别  
  4.  *readOnly :只读  
  5.  *rollbackFor :发生哪些异常回滚  
  6.  *noRollbackFor :发生哪些异常不回滚  
  7.  *rollbackForClassName 根据异常类名回滚  
  8.  */  
  9. @Transactional  
  10. public class AccountServiceImpl implements AccountService {  
  11.   
  12. //注入转账的DAO  
  13. private AccountDao accountDao;  
  14.   
  15.   
  16.   
  17. /** 
  18. * @param out :转出账号 
  19. * @param in :转入账号 
  20. * @param money:转账金额 
  21. */  
  22. @Override  
  23. public void transfer( String out, String in, Double money) {  
  24. accountDao.outMoney(out, money);  
  25. //int i = 1/0;  
  26. accountDao.inMoney(in, money);  
  27.   
  28. }  
  29.   
  30.   
  31. public void setAccountDao(AccountDao accountDao) {  
  32. this.accountDao = accountDao;  
  33. }  
  34.   
  35.   
  36. }  


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="2296147" snippet_file_name="singit" name="code" class="java"><pre code_snippet_id="2296147" snippet_file_name="singit"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12.      
  13. </pre>  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="2296147" snippet_file_name="singit" name="code" class="java"><pre code_snippet_id="2296147" snippet_file_name="singit"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12.      
  13. </pre>  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="2296147" snippet_file_name="singit" name="code" class="java"><pre code_snippet_id="2296147" snippet_file_name="singit"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12.      
  13. </pre>  

声明:本篇文章经http://www.imooc.com/learn/478整理而出。如有侵权请联系博主,谢谢。
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="2296147" snippet_file_name="singit" name="code" class="java"><pre code_snippet_id="2296147" snippet_file_name="singit"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12.      
  13. </pre>  
端到端自动驾驶的精准测试场景开发 1. 背景介绍 自动驾驶技术的发展正在改变交通运输的未来,然而,要实现安全可靠的自动驾驶,需要进行大量的测试和验证。端到端自动驾驶系统的测试场景开发是实现这一目标的关键。本文将详细介绍端到端自动驾驶的精准测试场景开发,包括核心概念、算法原理、数学模型、项目实践,以及未来发展趋势。 2. 核心概念与联系 阅读详情

相关推荐

IMU卡尔曼滤波方法详细介绍

本文介绍了基于IMU和卡尔曼滤波的状态估计方法。系统采用9维状态向量(位置、速度、加速度计零偏),通过牛顿运动学建立状态方程,并利用外部位置观测进行校正。详细推导了状态转移矩阵和控制输入矩阵的形式,阐述了预测和更新的计算步骤。同时说明了过程噪声Q和观测噪声R的设置原则,其中Q反映模型不确定性,R取决于传感器精度。该方法可有效融合IMU测量与外部观测数据,实现位置、速度和偏置的联合估计。

haing2019的博客 757

一文带你了解Spring中的事务管理

声明式、编程式事务怎么选?spring事务管理底层源码解析、spring中事务是跟线程绑定的,事务只能控制当前线程的操作。

qq_42394044的博客 1336

2025.1.16——五、LoveSQL1 sqlmap文件类|万能密码

题目来源:BUUCTF[极客大挑战2019]LoveSQL1。

2402_87387800的博客 1067

Spring事务(4种特性、7种传播行为,5种隔离级别)

转载自https://blog.youkuaiyun.com/hcmony/article/details/77850183 Spring事务 事务是逻辑上的一组操作。组成这组操作的各个逻辑单元必须全部完成,如果有一个失败的话,那么事务就会回滚到最开始的状态,仿佛什么都没发生过一样。 事务有四个特性:ACID 原子性(Atomicity):事务是一个原子操作,由一系列动作组成。事务的原子性确保...

春卷同学的博客 8853

用VASP计算GW+BSE

VASP在5.3以后的版本都能用解BSE方程的方法,在计算光吸收的过程中考虑激子效应。 下面就来写一个用BSE计算bulkSi的例子 首先,介电函数的计算是在GW计算的基础之上的。这里首先介绍G0W0的计算(我们这里直接在PBE的波函数基础上进行G0W0计算)。 1.PBE自恰计算,写WAVECAR System= Silicon groundstate occupied

ultra1989的专栏 2万+

代谢组数据分析(二十):通过WGCNA识别核心代谢物

本教程详细介绍了一种系统性的分析流程,旨在从复杂的代谢物数据中识别出关键的代谢物。这一流程首先通过精心设计的代谢物选择步骤,从大量候选代谢物中筛选出具有潜在生物学意义的子集。随后,利用WGCNA方法,对这些筛选后的代谢物进行深入分析,以构建它们的共表达网络,最终筛选出与研究目的相关的核心代谢物(hub metabolites)。

专注生信领域 3771

Java 的事务详解

背景知识 事务,其实就是一个或者一组不会中途失败的操作。简单理解就是,要么同时成功,要么同时失败。一开始我对这句话有点不解,因为这句话有点故意押韵的意思,但是结合一个实际场景,就豁然开朗了。在网购的时候都需要通过网络支付才能完成下单,如果“付钱”这个动作没有完成,就显示“已支付”,那么就没有人会去网上卖东西。 事务的特性 根据上边的背景,我们可以总结出来事务的四个特性,也是面试中经常被问到...

老李写博客的地方 539

智能预测性维护AI系统实战效率提升:AI应用架构师的技巧分享

在工业领域,预测性维护(Predictive Maintenance, PdM)是降低停机损失、优化维护成本的关键手段。然而,现有系统往往面临三大效率瓶颈数据处理延迟:工业设备产生的海量实时数据(如传感器、PLC)需传输至云端处理,导致预警滞后;模型推理效率:复杂深度学习模型(如ResNet、Transformer)在边缘设备上推理时间长,无法满足实时要求;落地集成困难:与企业现有ERP、MES、SCADA系统对接繁琐,导致“模型好用但业务不用”的尴尬。本文从架构、数据、模型、落地架构设计。

AI天才研究院 744

Spring 事务失效的六种情况

转存失败重新上传取消[TOC]转存失败重新上传取消最近有小伙伴告诉松哥说面试中被问到这个问题了,不知道该怎么回答,这能忍?捋一篇文章和小伙伴们分享下吧。既然捋成文章,就连同 Spring 事务一起梳理下吧。数据库事务是指作为单个逻辑工作单元执行的一系列操作,这些操作要么一起成功,要么一起失败,是一个不可分割的工作单元。在我们日常工作中,涉及到事务的场景非常多,一个 service 中往往需要调用不同的 dao 层方法,这些方法要么同时成功要么同时失败,我们需要在 service 层确保这一点。说到事务最典型

BASK2311的博客 651

【49】软考软件设计师——冲刺复盘:高频易错点与应试策略|复盘清单+应试技巧

本文是《软件设计师·50讲通关|从零基础到工程师职称》专栏第49篇,属于**模块六:冲刺与模拟**终篇,聚焦考前最后阶段的“精准复盘+应试提分”。全文围绕“高频易错点清零、答题时间优化、应急处理落地”三大核心,梳理9大知识领域的典型易错点(附避坑技巧)、优化版答题时间分配(含取舍策略)、考中全场景应急方案,配套可直接使用的“复盘清单”和“应试技巧手册”,帮助考生在最后阶段查漏补缺、固化得分逻辑,避免因低级错误丢分,以“稳、准、快”的状态应对考试。下一篇第50篇(终章)将进行全专栏知识体系回顾、考试策略总结

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 440

Allegro如何设置走线自动删除重复走线操作指导

Allegro如何设置走线是自动删除重复走线操作指导在PCB布线的时候,点到点需要重复走一段线的时候,希望原来的线段自动被删除,Allegro设计的时候支持这个操作如下图,重复走中间的线段的时候,希望下方的走线自动删除具体操作如下This section is describe what the function allegro have ,helpfully could let user know more about allegroAllegro Design and Analysis includes

weixin_54787054的博客 4499

9.1 数据库事务概述

9.1  数据库事务概述http://www.cnblogs.com/duanxz/p/3788190.html 事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务。   事务必需满足ACID(原子性、一致性、隔离性和持久性)特性,缺一不可: 原子性(Atomicity):即事务是不可分割的最小工作单元,事

九天工作室 2157

qq对接openclaw,通过qq发送命令

把 OpenClaw 接入 QQ 和微信,目前主流的微信文章和教程主要提供了两种截然不同的路径。我为你梳理了它们的核心区别和关键步骤,你可以根据自己的需求来选择。

suny8的专栏 270

Spring事务管理详解

Spring事务管理详解

一个普本码农奋斗史 8万+

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【Spring】Spring的事务管理

该接口是Spring提供平台事务管理器,主要用于管理事务状态事务TransactionStatus getTransaction(TransactionDefinition definition):获取事务的状态信息void commit(TransactionStatus status):用于提交事务void rollback(TransactionStatus status):用于回滚事务该接口并不了解具体实现类,常用如下。

m0_62645012的博客 2068

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

说说Spring事务管理

实现了业务逻辑与事务控制的解耦。Spring 事务管理是 Spring 框架的核心功能之一,旨在简化数据库操作的原子性、一致性、隔离性和持久性(ACID)控制。原理:基于 AOP 动态代理,在运行时拦截@Transactional注解的方法,自动管理事务的开启、提交和回滚。嵌套事务回滚:REQUIRES_NEW的内层事务回滚不影响外层,而NESTED的内层回滚可被外层捕获。避免大事务:减少事务内耗时操作(如 RPC 调用),拆分为小事务。适用场景:需精细控制事务边界(如根据条件提交/回滚)。

tschen的博客 1271

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【Spring】— Spring的事务管理

Spring的事务管理简化了传统的事务管理流程,并且在一定程度上减少了开发者的工作量。

衍生星球的博客 3320

Spring事务管理总结

Spring事务管理机制概述 Spring事务管理模块主要包括3个接口 PlatformTransactionManager:事务管理器,主要用于平台相关的事务管理。 TransactionDefinition:事务定义信息(隔离级别、传播、超时、只读)通过配置如何进行事务管理。 TransactionStatus:事务具体运行状态——事务管理过程中,每个时间点事务的状态信息。 1、Platf...

qq_42961888的博客 695

Spring事务管理详解+案例

Spring事务管理详解Spring事务事务简介事务原则Java事务Java事务类型事务类型差异Spring事务核心接口 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200311210812187.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0...

SpringWaterLikeMe 1327

Spring事务管理

介绍了事务的概念,事务的特性,JDBC 事务管理的步骤和操作过程,以及Spring事务管理的两种实现方式:编程式事务管理和声明式事务管理。

m0_74808313的博客 1277

Spring 中事务管理详解

是一组操作的集合,这些操作要么全部成功,要么全部失败。事务必须遵循。

q68686的博客 1408

Spring 事务管理机制概述

用户的每次请求都对应一个业务逻辑方法,而一个业务逻辑方法往往包括一系列数据库原子访问操作,并且这些操作应绑定成一个事务来执行。使用传统事务编程策略时,程序代码必然和具体的事务操作代码耦合,而Spring事务管理策略可以避免这种尴尬。Spring提供了两种事务管理方式:编程式事务管理和声明式事务管理,本文在对Spring事务管理API分析的基础上,详细地阐述了Spring编程式事务管理和声明式事务管理的原理、本质和使用

Rico's Blogs 3万+

Spring中进行事务管理的两种方式

1、Spring中事务管理的API PlatformTransactionManager TransactionDefinition TransactionStatus 2、编程式事务管理 3、声明式事务管理 基于Spring-AOP的方式 基于AspectJ的标签 基于注解的方式

theVicTory的博客 4123

【Spring】Spring事务管理

1.1简介Spring框架内部使用事务管理器的接口,来完成commit和rollback针对不同的框架,提供不同的实现类1.2ACID属性。

wosixiaokeai的博客 1145

Spring的事务管理详解

Spring的事务管理详解 1、自定义的事务管理器 1.1 编程式事务 通过在业务层中注入事务管理器对象。然后通过编码的方式进行事务的控制 具体的配置 <!-- 使用DruidDataSource生成数据源,然后将其注入到spring容器中 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" v

lllms 869
上一篇: 【数据结构】——-栈、队列和数组(三)
下一篇: Java线程的概念
白衣染霜陈
博客等级 码龄22年 682粉丝 338原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值