对spring事物控制的不解

本文探讨了Spring提供的事务管理工具如TransactionProxyFactoryBean和TransactionInterceptor的应用,并讨论了如何在DAO和服务层实现事务控制。作者还分享了一种通过自定义基本DAO类来管理连接的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对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-class="true">
<aop:advisor pointcut="execution(* com.longtuo.server.bo..*Service.*(..))" 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.youkuaiyun.com/goodhumor/archive/2008/03/04/2144911.aspx
jdbctemplate事物控制
http://developer.51cto.com/art/200906/127430.htm
jdbctemplate使用
http://blog.youkuaiyun.com/nomads/archive/2006/05/05/709551.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值