spring 事务配置


spring 官方团队 建议我们使用注解方式 配置事务,这样可以做到精确配置,具体怎么精确,看个人理解,希望在评论处附上,我不明白这一点。
基于xml配置事务


<context:property-placeholder location="classpath:jdbc.properties"/>
	 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="${driverClassName}"/>
	    <property name="url" value="${url}"/>
	    <property name="username" value="${username}"/>
	    <property name="password" value="${password}"/>
	     <!-- 连接池启动时的初始值 -->
		 <property name="initialSize" value="${initialSize}"/>
		 <!-- 连接池的最大值 -->
		 <property name="maxActive" value="${maxActive}"/>
		 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
		 <property name="maxIdle" value="${maxIdle}"/>
		 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
		 <property name="minIdle" value="${minIdle}"/>
	 </bean>

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  	   <property name="dataSource" ref="dataSource"/>
    </bean>

	<aop:config>
	  	<aop:pointcut id="transactionPointcut" expression="execution(* cn.itcast.service..*.*(..))"/>
	  	<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
	</aop:config> 
	<tx:advice id="txAdvice" transaction-manager="txManager">
		  <tx:attributes>
		    <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
		    <tx:method name="*"/>
		  </tx:attributes>
	</tx:advice>

	<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
		<property name="dataSource" ref="dataSource"/>
	</bean>




基于注解配置事务

  @Transactional
public class PersonServiceBean implements PersonService {
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	// unchecked ,
	// checked
	@Transactional(noRollbackFor=RuntimeException.class)
	public void delete(Integer personid) throws Exception{
		jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
				new int[]{java.sql.Types.INTEGER});
		throw new RuntimeException("运行期例外");
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Person getPerson(Integer personid) {		
		return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, 
				new int[]{java.sql.Types.INTEGER}, new PersonRowMapper());
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());
	}

	public void save(Person person) {
		jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},
				new int[]{java.sql.Types.VARCHAR});
	}

	public void update(Person person) {
		jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()},
				new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}

你可能感兴趣的:(spring 事务)