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: 3879
posted
0
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
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: 3879
posted
0
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