Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean Managed Transactions

 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic