Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

con = getConnection()  RSS feed

 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I use WL 8, Win2000 ,Eclipse and Struts and EJBs.I have set up data source and connection pool in WL 8.
In my "EmploymentHistoryBean" class's methods , is it "okay" to write following statement to get connection object ? Once I get this "con" i pass it on to DAOs.

con = getConnection();

I am looking at this question from the perspective of design pattern.

thanks
-Anand.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand,
As long as getConnection() is getting the connection from the datasource/connection pool, that pattern is fine.
 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this statement goes in the business method !! I saw codes of programmers working for more than 6 years. They have done this....

But , I read a Wrox book "Professional Struts Applications" which considers this as "non professional" stratagy.

So I was little confused !!!
 
neeraj pandey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally you should have a DAO for database related direct queries. The bean acts as a facade and invokes the method defined in DAO which internally manages its connection. The pooling of connection is reposnsibility of server, so do the jndi lookup of the datasource and close the connection as soon as you are done.
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Came across the following

"Always pass database connection from business logic layer (not the EJB's which belong to the deployment layer)....DAO code should be application generic .." from J2EEArchitectsHandBook by Derek C Ashmore.

Actually, this was quite new to me. I too had been using the DAO layer for
acquiring and closing connections.

Any suggestions/ideas for this approach ?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how you look at it. I'd put the connection handling logic ie., retrieving a pooled connection and cleaning it up in the DAO since these are directly related to data access activities. It is about the division of labor. The bean takes care of business methods and dealing with data objects whereas the DAO layer hides the underlying database and raw data formats from the business tier. Since the intent of the DAO layer is to not only hide, but also to insulate the business tier from changes to data architecture, I'd even say that business tier should have no knowledge and/or dependency on anything to do with data other than the DAO layer.

This is how I've done it in the past - A DAOFactory creates DAO instances based on configured datasource/database type. EJB tier interacts with the DAOFactory to retrieve instances of DAO objects and simply execute methods for CRUD data operations. The DAO classes have a common type hierarchy and somewhere in a common ancestor class( DAOBase? ) is the logic to talk to JNDI, retrieve and cleanup connnections, handle common set of exceptions etc.
 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all !!
Ajith,
That way you are putting two layers "EJB" and "DAO" into weblogic app server right ??? If I am right , my question would be "How would you separate DAOs and EJBs ??"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!