wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Bean Managed Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Bean Managed Transactions" Watch "Bean Managed Transactions" New topic
Author

Bean Managed Transactions

Kiran Kumar
Ranch Hand

Joined: May 18, 2001
Posts: 107
i am having a session bean where i have bean managed transaction. an entity bean with bean managed persistance is also there. i want to update 3 tables, so i am having bean managed persistance for the entity bean. if i pass the database connection got from looking up a datasource as an argument to the create method of the entity bean along with other arguments will i be able to maintain transaction. the code is like
UserTransaction ut = context.getUserTransaction();
ut.begin();
UserEntity user = userentityhome.create("name",1234,Connection)


Thanks<br /> <br />Kiran <br /> <br />SCEA, SCJP 1.4,<br /> <br />"First they ignore u, then they laugh at u, then they fight u, <br />then u will win<br /> <br />Mahatma Gandhi"
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by kiran sonata:
i am having a session bean where i have bean managed transaction. an entity bean with bean managed persistance is also there. i want to update 3 tables, so i am having bean managed persistance for the entity bean. if i pass the database connection got from looking up a datasource as an argument to the create method of the entity bean along with other arguments will i be able to maintain transaction. the code is like
UserTransaction ut = context.getUserTransaction();
ut.begin();
UserEntity user = userentityhome.create("name",1234,Connection)

You should NOT have to do that. In all J2EE compliant Application SErvers, simply obtaining a new Connection from a DataSource inside your BMP should be enough to enlist it in the transaction started in your session bean. In fact, I would certainly NOT suggest that you do what you are doing -- you should ONLY pass in required fields to your Entity bean create methods -- and likewise you should NEVER store a Connection as part of your entity bean -- just obtain them from a DataSource within the ejbCreate(), ejbStore() and ejbLoad() methods.
Kyle

------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
By the way -- read Richard Monson-Haefel's book. It explains how the transactions work in excruciating detail.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Kyle Brown:
By the way -- read Richard Monson-Haefel's book. It explains how the transactions work in excruciating detail.
The link for that book is: http://www.amazon.com/exec/obidos/ASIN/0596002262/electricporkchop

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Kiran Kumar
Ranch Hand

Joined: May 18, 2001
Posts: 107
but the entity bean is bean managed persistance. so if i get a connection in the entity bean create method will it work.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
As I said earlier, yes it will work. As to passing connections around and caching them -- you DO NOT want to do this! Here's the problem -- at some point you will need to find your Entity bean using a findBy... method. How do you intend to pass the Connection to it then?
Just do what I suggested. Don't worry about passing a connection -- you do not need to do that. Instead, inside your ejbCreate() do the following:
ejbCreate() {
// Get an InitialContext (unique for your app server)
// Lookup your datasource
DataSource ds = (DataSource)ic.lookup("jdbc/MyDatasource");
// Get your connection
Connection conn = ds.getConnection();
// use the connection
// close the connection
conn.close();
}
Then do the same steps inside ejbLoad() and ejbStore() and all of your ejbFindBy... methods. That's all. You do not have to pass the connection around, and in fact you should not -- that's not following the way that EJB's work. Odd things would happen if you try to persist the connection -- you see the problem is that the container needs to manage the connections in order to ensure that all database manipulations happen in the same transaction. If you start caching the connections, then you're circumventing this management, potentially with disastrous results.
Please, sit down and read Richard Monson-Haefel's book. Then read the sections of the EJB specification on connection management and transactions. This will all be much clearer then.
Kyle

------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
[This message has been edited by Kyle Brown (edited November 24, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean Managed Transactions