dcsimg
Can't access database from session bean and create entity bean within the same transaction
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jan_Zamojski
Posted On:   Thursday, October 4, 2001 03:50 AM

I'd like to manage an ID/keyword table using EJBs. The table basically contains only two fields, a numerical (integer) ID and a keyword (String or VARCHAR in the database). Whenever I'm about to create a new entry I would like to obtain the new ID by incrementing the maximum value stored so far. I try to perform the following two steps in a stateless session bean: 1. Do a "Select MAX(ID)" from the table 2. Invoke the create() Method of the entity bean using + 1 as the new ID. Because I want everything to happen within one transaction I set TX_REQUIRED for the session bean and TX_MANDATORY for the entity bean. However I receive a TransactionRolledbackException (I use the VAJ WebSphere Test Environment). When I ch   More>>

I'd like to manage an ID/keyword table using EJBs. The table basically contains only two fields, a numerical (integer) ID and a keyword (String or VARCHAR in the database). Whenever I'm about to create a new entry I would like to obtain the new ID by incrementing the maximum value stored so far. I try to perform the following two steps in a stateless session bean:


1. Do a "Select MAX(ID)" from the table


2. Invoke the create() Method of the entity bean using + 1 as the new ID.


Because I want everything to happen within one transaction I set TX_REQUIRED for the session bean and TX_MANDATORY for the entity bean. However I receive a TransactionRolledbackException (I use the VAJ WebSphere Test Environment). When I change the transaction attributes so that the two steps are performed in separate transactions, everything works OK. What's wrong here? Is my trouble related to the fact that I open (and close) a new database Connection in my session bean?

Thanks for any hints.


Jan

   <<Less

Re: Can't access database from session bean and create entity bean within the same transaction

Posted By:   Jan_Zamojski  
Posted On:   Thursday, October 18, 2001 12:13 AM

Hello World,



Meanwhile the problem is solved - it turned out to be a configuration error within VAJava. Somehow (most probably due to my ignorance :-) it was using the wrong JDBC driver. Clearing all internal classpaths, deleting the JDBC driver from the workspace and setting the classpaths again to contain only the absolutely neccessary classes finally helped. Sorry for bothering the jGuru community with this problem.


Jan

Re: Can't access database from session bean and create entity bean within the same transaction

Posted By:   Bozidar_Dangubic  
Posted On:   Thursday, October 11, 2001 06:58 AM

well, first I think you should use TX_REQUIRED on both session bean and entity bean method. It just makes more sense and TX_MANDATORY is really not necessary in this case if you design everything properly. second, my question is why didn't you do both things in a single method invocation. it simply makes more sense to obtain an ID and do a store in the entity bean itself. if you need this id in the session bean (which I am guessing works as a facade to the entity beans), simply return it from the entity bean (you will probably have to anyway since ejbCreate() returns PK in entity beans.
About | Sitemap | Contact