invoke ejb ,JTA in tomcat server will get the error.

i use the tomcat server and a ejb server envirmoment.
some logic code is like these:

class ship
UserTransaction ut=null;
public void begin()
ut=new UserTransaction

public void add(String nn) throws Exception
invoke some ejb
save some data into database
public void commit()
public void rollback()

and this class will be save and invoke in tomcat jsp file.

a complete transaction is like these
step 1 )file1.jsp to start the transaction
2 )file2.jsp to add the data or detail ,like buy
information, product ....
3)file3.jsp to commit transaction or rollback transaction

tomcat ship in session
client1 --- connect-- Thread1 --- ship.begin() file1.jsp
cleint2---------------- Thread2 --- ship.begin() file1.jsp
client1 ---------------Thread3 ----- ship.add("aa") file2.jsp
client2--------------- thread2------ship.add("aa") file2.jsp
client2-----------------Thread2-----ship.commit() file3.jsp

i had did the test ,and confirm the client cant confirm get the some thread in tomcat server.

if like the client2 will not get any error and can execute sucessful.
but the tomcat Thread pool cant control by jboss .
so how does it solve this problem??
thanks you very much.
because i try to implement a software like ejb server ,and self yo provider the distrubtion transaction function. and i got this problem, hope you can help me to solve it.thank you

The crux of your problem is to provide a transaction control which span several threads.(each invocation of *.jsp means a new thread.) Although such transaction is allowed by means of stateful-session-bean, the usage is strongly discouraged for the reason of its error-prone. U'd better refactor your code and finish the transaction in single thread.

by the way, could u tell the problem in chinese? it's quite tough to dig out your problem! :)

Sorry,cos in my company not the chinese system can type the chinese.

so that mean we cant use the transaction in many threads ,or can invoke it in single thread? is it like that?

and how the stateful-session bean working in the JTA.
can give some code or examples.
thanks you very much.

My Advice: NEVER make the transaction span several threads!

You can maintain the JDBC or JTA transaction in Stateful Session Bean.
The following is extracted from j2ee1.4 tutorial:


Returning without Committing
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.

看了一下你的应用,发现你对Stateful session bean和事务理解有偏差:在tomcat上,每个客户必须有一个单独的Stateful Session Bean为其服务。两个客户用一个Stateful Session Bean不make sense!你可以为每个client的http session维护一个attribute,存储Stateful session Bean的引用。每个Stateful session Bean维护一个Transaction,分别为不同客户服务。

每个client的http session维护一个attribute,存储Stateful session Bean的引用。每个Stateful session Bean维护一个Transaction,分别为不同客户服务.i have do it like that,but my problem is ,however the stateful session bean need span a few jsp page to finsh a transaction ,JTA will error or my mind will get the error .it is because the JTA 是用Thread 来邦定transaction的呀。
但是在我的实验中,http session maintain a attribute ,and save the stateful session bean 的引用,将不会每次都在同一Thread 。

eg)a business logic .need 3 step to finish it.
1 step (1.jsp )
create a stateful session bean ,
start the transaction (begin),
save it into http session.
2 step (2.jsp)
get the stateful session bean ,
invoke it and save some data to database,
3 step (3.jsp)
get the stateful session bean,
commit or rollback the transaction.

client1 将不会每次都在同一个thread 内,它将有机会出现在不同的thread .所以jta 可能会出现error.

in my testing ,client 1 stateful session bean have the change invoke in different thread.
eg)many client connect this sample.client 1 get the thread 1 with the tomcat,client 2 get the thread 3 ,but the tomcat will general a thread pool. so maybe client 1 can alawys use get the thread1 in 2 step or 3 step (in this time,session will not error) ,but if unlucky client 2 in step get the thread is 2 and step 2 it get the thread is 1 with the tomcat (in this time, jta still can working probability).

because i try to do a research and implements the jta program .so that i get this problem, 因此,我想知道jta 是否有此问题。如有他们如何解决。
thanks you very much.



to abayi
我个人认为,你这个案例可以使用SessionSynchronization SFSB 实现,无必要在web层使用JTA。

如果你的ejb server是两台机器,这篇文章讨论对你可以借鉴:

补充:我的意思是:如果你不是跨EJB服务器操作,不必在web中使用JTA,完全可以使用SFSB + CMT或BMT实现。

The J2EE transaction manager does have one limitation, however, it does not support true nested transactions(真正的嵌套事务). You can begin a transaction within another transaction, however, when commit is called on the inner transaction, it is final and there is no way to roll it back C even if the outer transaction rolls back.

i feel very sorry first,because my english is very poor ,so maybe your can not understand what i mean.
i think say like this can help your to understand my mean,
跨span several jsp 操作 stateful session ejb ,but the session ejb is include a jta operate.and transaction method of begin ,commit and rollback is execute in not equel jsp file.
will get the error ?it is because the jta operate is let the thread link to the connection ,but the problem is web container cant ensure the operate in this few jsp in a same thread.
so how to solve this problem????

to banq :
can you explain "SessionSynchronization SFSB " this thing to me know .cos i still not very understand this concept.

to 廉价劳力 :
i want do it like that ,because is easy let the jsp deverloper easy invoke the data and can direct save to database not need coding more code to control the temp object in memory.

thanks all friends response me ,and discuss with me.

to abayi
1. jsp --> JTA -- > SFSB
2. jsp ---> SFSB ---> JTA

我认为你说的是第一种情况,第二种情况在J2EE 1.4 spec中以及说明了,这种情况的使用是非常耗费性能的,因为事务一直存在,SFSB可能需要钝化到磁盘,这种情况下性能不好。