spring基於XML的聲明式事務控制-配置步驟java
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd"> <!-- 配置業務層--> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置帳戶的持久層--> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置數據源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property> <property name="username" value="root"></property> <property name="password" value="1234"></property> </bean> <!-- spring中基於XML的聲明式事務控制配置步驟 一、配置事務管理器 二、配置事務的通知 此時咱們須要導入事務的約束 tx名稱空間和約束,同時也須要aop的 使用tx:advice標籤配置事務通知 屬性: id:給事務通知起一個惟一標識 transaction-manager:給事務通知提供一個事務管理器引用 三、配置AOP中的通用切入點表達式 四、創建事務通知和切入點表達式的對應關係 五、配置事務的屬性 是在事務的通知tx:advice標籤的內部 --> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事務的通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 配置事務的屬性 isolation:用於指定事務的隔離級別。默認值是DEFAULT,表示使用數據庫的默認隔離級別。 propagation:用於指定事務的傳播行爲。默認值是REQUIRED,表示必定會有事務,增刪改的選擇。查詢方法能夠選擇SUPPORTS。 read-only:用於指定事務是否只讀。只有查詢方法才能設置爲true。默認值是false,表示讀寫。 timeout:用於指定事務的超時時間,默認值是-1,表示永不超時。若是指定了數值,以秒爲單位。 rollback-for:用於指定一個異常,當產生該異常時,事務回滾,產生其餘異常時,事務不回滾。沒有默認值。表示任何異常都回滾。 no-rollback-for:用於指定一個異常,當產生該異常時,事務不回滾,產生其餘異常時事務回滾。沒有默認值。表示任何異常都回滾。 --> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!-- 配置aop--> <aop:config> <!-- 配置切入點表達式--> <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut> <!--創建切入點表達式和事務通知的對應關係 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor> </aop:config> </beans>
/** * 帳戶的業務層實現類 * * 事務控制應該都是在業務層 */ public class AccountServiceImpl implements IAccountService{ private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } @Override public void transfer(String sourceName, String targetName, Float money) { System.out.println("transfer...."); //2.1根據名稱查詢轉出帳戶 Account source = accountDao.findAccountByName(sourceName); //2.2根據名稱查詢轉入帳戶 Account target = accountDao.findAccountByName(targetName); //2.3轉出帳戶減錢 source.setMoney(source.getMoney()-money); //2.4轉入帳戶加錢 target.setMoney(target.getMoney()+money); //2.5更新轉出帳戶 accountDao.updateAccount(source); int i=1/0; //2.6更新轉入帳戶 accountDao.updateAccount(target); } }
spring基於註解的聲明式事務控制mysql
<?xml version="1.0" encoding="UTF-8"?> -<beans xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"> <!-- 配置spring建立容器時要掃描的包--> <context:component-scan base-package="com.itheima"/> <!-- 配置JdbcTemplate--> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> <property ref="dataSource" name="dataSource"/> </bean> <!-- 配置數據源--> -<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/eesy"/> <property name="username" value="root"/> <property name="password" value="1234"/> </bean> <!-- spring中基於註解 的聲明式事務控制配置步驟 一、配置事務管理器 二、開啓spring對註解事務的支持 三、在須要事務支持的地方使用@Transactional註解 --> <!-- 配置事務管理器 --> -<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property ref="dataSource" name="dataSource"/> </bean> <!-- 開啓spring對註解事務的支持--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
/** * 帳戶的業務層實現類 * * 事務控制應該都是在業務層 */ @Service("accountService") @Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只讀型事務的配置 public class AccountServiceImpl implements IAccountService{ @Autowired private IAccountDao accountDao; @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } //須要的是讀寫型事務配置 @Transactional(propagation= Propagation.REQUIRED,readOnly=false) @Override public void transfer(String sourceName, String targetName, Float money) { System.out.println("transfer...."); //2.1根據名稱查詢轉出帳戶 Account source = accountDao.findAccountByName(sourceName); //2.2根據名稱查詢轉入帳戶 Account target = accountDao.findAccountByName(targetName); //2.3轉出帳戶減錢 source.setMoney(source.getMoney()-money); //2.4轉入帳戶加錢 target.setMoney(target.getMoney()+money); //2.5更新轉出帳戶 accountDao.updateAccount(source); int i=1/0; //2.6更新轉入帳戶 accountDao.updateAccount(target); } }
spring基於純註解的聲明式事務控制spring
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/eesy jdbc.username=root jdbc.password=1234
/** * 和鏈接數據庫相關的配置類 */ public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 建立JdbcTemplate * @param dataSource * @return */ @Bean(name="jdbcTemplate") public JdbcTemplate createJdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); } /** * 建立數據源對象 * @return */ @Bean(name="dataSource") public DataSource createDataSource(){ DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; } }
/** * spring的配置類,至關於bean.xml */ @Configuration @ComponentScan("com.itheima") @Import({JdbcConfig.class,TransactionConfig.class}) @PropertySource("jdbcConfig.properties") @EnableTransactionManagement//開啓事務管理 public class SpringConfiguration { }
/** * 和事務相關的配置類 */ public class TransactionConfig { /** * 用於建立事務管理器對象 * @param dataSource * @return */ @Bean(name="transactionManager") public PlatformTransactionManager createTransactionManager(DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
/** * 帳戶的業務層實現類 * * 事務控制應該都是在業務層 */ @Service("accountService") @Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只讀型事務的配置 public class AccountServiceImpl implements IAccountService{ @Autowired private IAccountDao accountDao; @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } //須要的是讀寫型事務配置 @Transactional(propagation= Propagation.REQUIRED,readOnly=false) @Override public void transfer(String sourceName, String targetName, Float money) { System.out.println("transfer...."); //2.1根據名稱查詢轉出帳戶 Account source = accountDao.findAccountByName(sourceName); //2.2根據名稱查詢轉入帳戶 Account target = accountDao.findAccountByName(targetName); //2.3轉出帳戶減錢 source.setMoney(source.getMoney()-money); //2.4轉入帳戶加錢 target.setMoney(target.getMoney()+money); //2.5更新轉出帳戶 accountDao.updateAccount(source); // int i=1/0; //2.6更新轉入帳戶 accountDao.updateAccount(target); } }