首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

对spring事物统制的不解

2012-09-18 
对spring事物控制的不解对spring事物控制的不解spring提供了代理TransactionProxyFactoryBean和拦截器Tran

对spring事物控制的不解
对spring事物控制的不解

spring提供了代理TransactionProxyFactoryBean和拦截器TransactionInterceptor对dao进行事物控制。
但这些都是针对dao方法的控制。也就是说在一个更新方法中(insert或者update等等)利用jdbctemplate或者hibernate更新多张表,可以利用aop做到事物控制。

但个人对dao的理解是一个dao对应一张表的全部操作,不应该增加其他表的操作。
这样可以做到耦合低和最大程度的复用。
在service中调用不同的dao进行更新,不同的dao之间实现事务控制。
看了下spring的源码,想自己调用DataSourceUtils,但貌似不行。

考虑将connection交由service处理,同一个connection参数传递给dao,处理完成后在service中提交或者回滚。
但service有可能操作不同的数据库或者用户,就会有多个connection或者datasource。
用这种方式感觉很别扭。

还有就是为什么要使用jdbctemplate和hibernate。老用的话,以后还会写sql么?
jdbctemplate是spring对jdbc的封装,省去了很多代码。但填充数据需要callback函数,当然不是很难写。
hibernate需要写配置文件,很烦。
求真相。。。
<aop:config proxy-target-advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com.longtuo.role.bo..*Service.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> 
这种将transactionManager交由service或者aop做的方式我们在用。但是感觉有点别扭,但是aop的方式能接受。

自己写了个抽象类,dao都继承这个类,感觉挺好用的。

public abstract class BasicDao {

private String alias = null;
protected Connection connection = null;
protected PreparedStatement preparedStatement = null;
protected CallableStatement callableStatement = null;
protected ResultSet resultSet = null;

protected void createQueryConfig(String sql) throws Exception{
connection = DriverManager.getConnection("proxool." + alias);
preparedStatement = connection.prepareStatement(sql);
}

protected void createProcedureConfig(String procedure) throws Exception{
connection = DriverManager.getConnection("proxool." + alias);
callableStatement = connection.prepareCall(procedure);
}

protected void releaseQueryConfig() {
try {
if (connection != null) {
connection.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}

protected void releaseProcedureConfig() {
try {
if (connection != null) {
connection.close();
}
if (callableStatement != null) {
callableStatement.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}

示例:
try {
this.createQueryConfig(sql);
this.preparedStatement.setString(1, cn);
this.preparedStatement.setInt(2, operation);
this.preparedStatement.setString(3, date);
this.resultSet = this.preparedStatement.executeQuery();
if (resultSet.next()) {
returnValue = resultSet.getInt(1);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.releaseQueryConfig();
}


文章记录:
spring事物配置
http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
spring proxool配置
http://blog.csdn.net/goodhumor/archive/2008/03/04/2144911.aspx
jdbctemplate事物控制
http://developer.51cto.com/art/200906/127430.htm
jdbctemplate使用
http://blog.csdn.net/nomads/archive/2006/05/05/709551.aspx

热点排行