• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where should the connection be obtained in the EJB

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a general doubt in stateless session beans.

Manning in Action for EJB3 states that "The JDBC connection object used to create the statement is a classic heavy duty resource. It is expensive to open and should be shared across cals wherever possible".

It further goes on to mention that connection should be opened in PostConstruct and closed in Predestroy callbacks methods of the bean.

However, i think closing the connection is equivalent to committing the transaction(not sure if it is right).
That way, do the transactions are committed only when the bean is destroyed(this is not true, so the above statement must be wrong).

Can someone shed some light on the above.

Also, if we have 10 stateless beans in the pool, in the method ready state, all of them will have opened the DB connections(if the connections are opened in PostConstruct).
Wouldn't it be undesirable because there are 10 open connections with the DB, when none of the transaction is active.

Please let me know.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul...


However, i think closing the connection is equivalent to committing the transaction(not sure if it is right).
That way, do the transactions are committed only when the bean is destroyed(this is not true, so the above statement must be wrong).


i think your first statement is wrong..
the connection is used to do data manipulation in database (also used to do transaction)..
the PostConstruct call method is used to get the resources needed by Stateless Session Bean..
and the bean's PreDestroy callback method is used to "clean" the resources that used by Stateless Session Bean..


Also, if we have 10 stateless beans in the pool, in the method ready state, all of them will have opened the DB connections(if the connections are opened in PostConstruct).
Wouldn't it be undesirable because there are 10 open connections with the DB, when none of the transaction is active.


in real, the EJB uses ConnectionPooling..
so, it doesn't matter if there are 10open connection with the DB, because that is pooled..
and the EJB container is having a job to instantiate the new instance of EJB (if there's no Bean in State ready)..
and the EJB container is having a job to give back the EJB in state ready..

Hope this help..
Please correct me if i'm wrong..

Thanks..
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leonardo,

Thanks for the prompt response...

I think there is a slight difference in what i am trying to ask is whether opening and closing DB the connection in PostContruct and Predestroy is the right strategy as mentioned in Manning In Action Series(if i understand it correctly)
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

if you use only JDBC, i think its the right strategy..
as i mentioned in my first post, the PostConstruct method is used to get the resource (get the DB Connection)..
the DB connection is also mentioned as resource..
the PreDestroy is used to clean resource (used to close the DB Connection)..

Thanks..
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonardo Carreira wrote:Hi..

if you use only JDBC, i think its the right strategy..
as i mentioned in my first post, the PostConstruct method is used to get the resource (get the DB Connection)..
the DB connection is also mentioned as resource..
the PreDestroy is used to clean resource (used to close the DB Connection)..



I am not sure..

In the beans where transaction handling is taken care by the container, the transaction commits when the connection is closed. Is it correct?
If no, then when does the transaction commit take place?
 
Moayad Abu Jaber
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you used EJB3 and dependency injection. the opening connection and closing doesn't your job. cause the container offer to you to handle that.

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moayad Abu Jaber wrote:if you used EJB3 and dependency injection. the opening connection and closing doesn't your job. cause the container offer to you to handle that.



I dont think the container will handle the opening and closing of connections...
It will only handle the injection of the Datasource..
 
Moayad Abu Jaber
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know you asked or guessed !! we here share the experience,then if you don't thing that you should not ask that !

handle that from you side as you want and as what you guessed.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul Babbar..

Yes, if we're talking about the Dependency injection, the scenario would be different..
we could use several annotation offered by EJB container..
there are many annotations could be used, such as :
@PersistenceContext //used while we use JPA to do data manipulation in database
@Resource // used while we want to use resources that provided, such as JNDI (could be use to get connection also)..
and so on..

Thanks..
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will someone please try to understand the question again....

1) Is opening and closing the connections in PostConstruct and Predestroy of a stateless session bean a good idea? Can someone explain with reasons?
2) In a stateless session bean, when do the transactions get committed or rollbacked? Is it when the connection is closed or during some other time?

Thanks & Regards,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!