日期:2014-05-18 浏览次数:20708 次
@Transactional(propagation=Propagation.REQUIRED)
public Blog save(Blog blog) {
//持久化,可能出现数据库异常,如主键约束,字段过大
dao.save(blog);
//发布 新增博客 事件,订阅者有 索引服务,日志服务,计数服务
fireEvent(blogEvent);//可同步或异步处理事件
return blog;
}
public Blog proxySave(Blog blog){
//事务层save
save(blogEvent); //有影响事务 计数服务 放在里面
//发布 新增博客 事件,订阅者有 索引服务,日志服务
fireEvent(blogEvent);
return blog;
}
public class OnCallDataSource implements DataSource {
private DataSource dataSource;
public OnCallDataSource(DataSource dataSource) {
this.dataSource = dataSource;
DataSourceUtil.OnCallHouse.allowCall();
}
public Connection getConnection() throws SQLException {
return new OnCallConnection(dataSource.getConnection());
}
//其他方法都是直接委托给原来的DataSource.
}
public class OnCallConnection implements Connection {
private Connection conn;
public OnCallConnection(Connection conn) throws SQLException {
this.conn = conn;
this.conn.setAutoCommit(false);
}
public void commit() throws SQLException {
this.conn.commit();
executeCalls(
DataSourceUtil.OnCallHouse.getCalls(
DataSourceUtil.OnCallHouse.COMMIT_CALLS));
}
public void rollback() throws SQLException {
this.conn.rollback();
executeCalls(
DataSourceUtil.OnCallHouse.getCalls(
DataSourceUtil.OnCallHouse.ROLLBACK_CALLS));