To jrog:

我试验了一下,确实如你所说,在同一个Transaction Context下,连续执行对同一个表的更新是不会引发“无限等待的”,所以我所说的那种问题应该是不存在的,而且手动的去写数据恢复代码的确是有够笨的:$(看来我得恶补一下Oracle常识了:))

我们的程序之所以出现“死锁”,估计是别的问题引起的,看来只有仔细调查后才知道原因了。

非常感谢你不厌其烦的解答,至少让我明白了JTA/JTS的基本原理。谢谢!

深圳金碟的大哥,非常感谢你提供的源码,让我更深地了解了事务:)

老先生,您真牛!


从何地方得知我是金蝶的?好生奇怪!

:)

to wwlhp@jdon.com,


另外小声问一句:金蝶是什么东西呀?:-) :(

Thread与Connection绑定,这里是不是可以用ThreadLocal?

本人认为Dino Santiago 存在一些误区。在同一个事务中,所执行的sql操作,是可以的,并不是说你插入一条数据A,一定要等到它提交了才能够操作,而是可以直接操作这条虽然还没有更新到数据库中的数据。至少在同一个事务中是这样的。

james.jia说的对,不过我已经知道了(Oracle常识不过关呐) :)
还是很谢谢你的提醒!

我以前也写过数据库连接和线程Map在一起的程序,不过我的程序不是web程序而是桌面应用程序,数据库连接池也是我自己实现的,不过我的程序是长期运行的,而且有些线程也是长期存在的,可是由于网络原因运行了一两个月后有时候connection坏了,甚至数据库驱动都无法再建立出一个新的connection了,我一直想知道web Container是如何管理数据库连接池的???请赐教!谢谢

如果你的事务不是两段的,根本没必要用JTA
直接用oracle的事务就可以处理。
如果实现自己的UserTransaction必须要使用XA Driver
还有实现底层控制,何况连J2EE的BMP效率都被认为太低
我看还是老老实实用数据库事务算了。
只要保证所有的操作使用同一个Connection
就可以保证事务的完整性
不过jrog的文也让我受益很多,thanks

偶尔看了上面的讨论,我个人发表点观点。
其实前面的程序中的无限制等待对于各个程序都是不同的,如果那两个更新的函数编写没有问题(如数据库连接耗尽引起的等待之类的),那就不会造成阻塞而无限制等待。

正常情况下,一般程序在UT中产生异常就应该导致transaction的回滚,而嵌套的transaction中内层的transaction是会检查是否有外层的transaction而不会在你程序指定commit的地方进行commit,直到最外层的transaction被提交,所以前面讨论的autoCommit(true)的情况大可不必担心。
autoCommit(true);
doA();
的等价代码等于:
startUT();();//这句话会被manager忽略因为外层的T
doA();
commitUT();//这句话会被manager忽略因为外层的T
如果想测试这种情况,可以用些ide进入调试模式,编写一些特殊代码试试看。

我觉得你的使用有问题。其实J2EE对事物提供两种方式:
1,容器处理的.EJB使用这种,CMP必须使用他,通过部署描述符来说明
2,客户端自己控制事物
这也有两种1,Connection控制,2,就是使用JTA
从你的程序来看把两种混淆在一起使用,不错才怪。


我在这里顺便提点小建议:希望大家在讨论问题的时候注意用词不要对公司或者个人进行攻击。

拜托,JDBC Transaction 和 JTA transaction不能混用的。这是基本常识阿

i some experience is about the dead lock.
i think your program alawys request in a same record in database.
my status in last time:
//database mysql -innodb type
id data name
1 1 a
2 1 b


and my program alawys update the record 1 data in database ,but all is under the transaction.
when you have many transaction is open and want update the same record 1 ,the dead lock state will be throws in your program.
(because have thread want use the a resource. maybe you can use syschronization to solve it.)
that is my last time state lah.
so i change my database this table to not innodb support,and then i not get the dead lock again in this program again.

so that i think your problem is same with me .
hope can help you.
and i feel very sorry cos my english is very poor ,maybe your cant understand what i mean.

能否提供一个JTA应用的简单例子。
正如jrog所说,UserTransaction必须和相应的DataSource联合使用。以weblogic为例,有没有从DataSource配置到UserTransaction调用的全过程例子。谢谢。

rogerathp 是对的。
In a stateless session bean with bean-managed transactions, a business method must commit or roll back a transaction before returning. However, a stateful session bean does not have this restriction.

In a stateful session bean with a JTA transaction, the association between the bean instance and the transaction is retained across multiple client calls. Even if each business method called by the client opens and closes the database connection, the association is retained until the instance completes the transaction.

In a stateful session bean with a JDBC transaction, the JDBC connection retains the association between the bean instance and the transaction across multiple calls. If the connection is closed, the association is not retained.