java 开源事务管理_xjl:Spring 事务管理原理探究

Spring 事务管理创造性的解决了很多以前要用重量级的应用服务器才能解决的事务问题,那么其实现原理一定很深奥吧?可是如果读者仔细研究了Spring事务管理的代码以后就会发现,事务管理其实也是如此简单的事情。这也印证了在本书开头的一句话“重剑无锋、大巧不工”,Spring并没有使用什么特殊的API,它运行的原理就是事务的原理。下面是DataSourceTransactionManager的启动事务用的代码(经简化):

protected void doBegin(Object transaction, TransactionDefinition definition)

{

DataSourceTransactionObject txObject =

(DataSourceTransactionObject) transaction;

Connection con = null;

try

{

if (txObject.getConnectionHolder() == null)

{

Connection newCon = this.dataSource.getConnection();

txObject.setConnectionHolder(

new ConnectionHolder(newCon), true);

}

txObject.getConnectionHolder()

.setSynchronizedWithTransaction(true);

con = txObject.getConnectionHolder().getConnection();

Integer previousIsolationLevel = DataSourceUtils

.prepareConnectionForTransaction(con, definition);

txObject.setPreviousIsolationLevel(previousIsolationLevel);

if (con.getAutoCommit())

{

txObject.setMustRestoreAutoCommit(true);

con.setAutoCommit(false);

}

txObject.getConnectionHolder().setTransactionActive(true);

// Bind the session holder to the thread.

if (txObject.isNewConnectionHolder())

{

TransactionSynchronizationManager.bindResource(

getDataSource(),txObject.getConnectionHolder());

}

}

catch (SQLException ex)

{

DataSourceUtils.releaseConnection(con, this.dataSource);

throw new CannotCreateTransactionException(

"Could not open JDBC Connection for transaction", ex);

}

}

本文出自:http://www.cownew.com

在调用一个需要事务的组件的时候,管理器首先判断当前调用(即当前线程)有没有一个事务,如果没有事务则启动一个事务,并把事务与当前线程绑定。Spring使用TransactionSynchronizationManager的bindResource方法将当前线程与一个事务绑定,采用的方式就是ThreadLocal,这可以从TransactionSynchronizationManager类的代码看出。

public abstract class TransactionSynchronizationManager

{

……

private static final ThreadLocal currentTransactionName = new ThreadLocal();

private static final ThreadLocal currentTransactionReadOnly = new ThreadLocal();

private static final ThreadLocal actualTransactionActive = new ThreadLocal(); ……

}

从doBegin的代码中可以看到在启动事务的时候,如果Connection是的自动提交的(也就是getAutoCommit()方法返回true)则事务管理就会失效,所以首先要调用setAutoCommit(false)方法将其改为非自动提交的。setAutoCommit(false)这个动作在有的JDBC驱动中会非常耗时,所以最好在配置数据源的时候就将“autoCommit”属性配置为true。

帮我把以下数据变成json格式:{ "switch_Cash": "Y", "pageMark": "add", "tx_date": "20230305", "cur_no": "1", "sfk_type": "03", "lease_type": "经营租赁", "change_rate": "0", "cap_name": "20220731SYZC000271", "lease_no": "20220731SYZC000271", "contract_key": "6980120220731001", "contract_name": "测试合同20220731-01", "bef_arch_area": "300", "bef_cash_rate: 2.38, "bill_type: 3, "bef_tax_rate: 5, "sfk_term: 4, "bef_tot_pay_time: 2, "bef_eve_amt: 22500, "exe_renew: N, "bef_renew_amt: 0, "bef_renew_eve_amt: 0, "bef_tot_pay_amt: 19047.62, "bef_unset_fin_exp: 221.39, "bef_contract_amt: 45000, "sige_date: 20220731, "bef_arch_position: 广州省佛山市宝芝林, "bef_effect_date: 20220801, "bef_mtr_date: 20240730, "bef_first_fk_date: 20220731,, "bef_renew_eft_date: , bef_renew_mtr_date: , aft_arch_area: 300, aft_bill_type: 3, aft_tax_rate: 5, aft_levy_rate: 5, aft_cash_rate: 2.38, aft_sfk_type: 03, aft_exe_renew: N, is_onchange: N, aft_sfk_term: 4, aft_contract_amt: 45000.00, aft_tot_pay_time: 2, aft_eve_amt: 22500, bef_renew_tot_pay_time: 0, max_term_seqn: 0, aft_effect_date: 20220801, aft_mtr_date: 20240730, aft_first_fk_date: 20220731, aft_renew_eft_date: , aft_renew_mtr_date: , bef_levy_rate: 5, cap_code: fjr0901, lease_no1: 20220731SYZC000271, aft_tot_pay_amt: 42857.14, aft_unset_fin_exp: 184.48999999999796, apply_amt: 23809.52, remark: test0609, change_valid_date: 20230609, cap_name_zh_: 测试资产-20230731-01, cur_no_zh_: 人民币, sfk_type_zh_: 周期性, flowable_: 1, flowable_targetNodeData: {"category":"CWSYS_69","isInput":"1","menuName":"使用权资产变更","pageNo":"cwsys_05_07"}, flowable_file_dataInfo: {"flowable_file_storeType":"undefined","flowable_file_split":false,"flowable_file_canDelete":true,"flowable_file_readOnly":false,"flowable_file_id":"FL-495eabb0-069c-11ee-9b92-59e9b66c35e6"}, cashList: [{"contract_no":"20220731HTXX000001","pay_date":"20220731","no":"20230201XJL000031","pay_amt":"25000.00","term_start_date":"20220801","create_trace_no":"16","tx_date":"0","trace_no":"0","term_end_date":"20230731","pay_sts":"未支付","version":"1","tax_rate":"0.00","term_seqn":"1","tax_amt":"1190.48","pay_sts_val":"0","id":"1","create_date":"20230201","levy_rate":"0.00"},{"contract_no":"20220731HTXX000001","pay_date":"20230731","no":"20230201XJL000032","pay_amt":"20000.00","term_start_date":"20230731","create_trace_no":"16","tx_date":"0","trace_no":"0","term_end_date":"20240730","pay_sts":"未支付","version":"1","tax_rate":"0.00","term_seqn":"2","tax_amt":"952.38","pay_sts_val":"0","id":"1","create_date":"20230201","levy_rate":"0.00"}] }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值