Spring事物处理(代码实现)

TransactionTemplate这个类是Spring的事务处理模板,在它的execute()方法里定义事务处理的骨架代码.

但execute()方法的TransactionCallback参数却是个接口,在这接口中定义了doInTransaction()方法

只要实现TransactionCallback接口,并在doInTransaction()方法里编写具体要进行的事务处理的代码就可以 了.

代码截图

 

 

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.sunflower.entity.People;


public class BankDaoImp implements BankDao {
	private JdbcTemplate jdbcTemplate;
	private TransactionTemplate transactionTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	@Override
	public double getMoney(final People people) {
		double money = people.getMoney();
		// 开始事务,如果出现状况则回滚
		transactionTemplate.execute(new TransactionCallback<People>() {
			@Override
			public People doInTransaction(TransactionStatus ts) {
				try {
					final People people2 = new People();
					// 使用JdbcTemplate进行持久化层操作
					String sql = "select money from bank where name = ?";
					Object[] params = new Object[] { people.getName() };
					// 查询
					jdbcTemplate.query(sql, params, new RowCallbackHandler() {
						@Override
						public void processRow(ResultSet rs)
								throws SQLException {
							people2.setMoney(rs.getDouble("money"));
							System.out.println(people.getName() + "用户还有"
									+ rs.getDouble("money") + "元余款");
							System.out.println(people.getName() + "要从账户中取出"
									+ people.getMoney() + "元");
							if (people2.getMoney() < people.getMoney()) {
								System.out.println("余额不足");
								people.setMoney(-1);
								return;
							}
						}
					});

					if (people.getMoney() < 0)
						return null;
					else {
						sql = "update bank set money = ? where name = ?";
						Object[] params2 = new Object[] {
								people2.getMoney() - people.getMoney(),
								people.getName() };
						jdbcTemplate.update(sql, params2);
						System.out.println("剩余余额:"
								+ (people2.getMoney() - people.getMoney()));
					}
				} catch (Exception e) {
					ts.setRollbackOnly();
				}

				// 如果成功,事务被提交
				return people;
			}
		});

		return people.getMoney();
	}
}

 

转载于:https://my.oschina.net/u/1047398/blog/850609

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值