I would like to know if a session bean can access the database via JDBC. If so, how do I get the connection?
- Session Bean accessing JDBC by Martin Straus on December 06 2004 10:22 EST
- Session Bean accessing JDBC by Mohan Rayavarapu on December 06 2004 11:53 EST
Yes, a session EJB can access a database through JDBC.
The usual way to implement this is using a DataSource, in order to delegate pool management and etcetera to the apps server.
How to configure a DataSource, depends on your app server. In Weblogic you must define connection pools and DataSources using them. In JBoss you must write a descriptor and deploy it the same way you deploy an EAR.
Hope it helps.
I think you should look at the Data Access Object Core J2EE Pattern
Background information is here:
When you're ready to start producing DAO code, you might prefer to generate it rather than write by hand.
FireStorm/DAO Java code generator imports database schema definitions from from live databases via JDBC and then generates a complete persistence tier based on the Data Access Object design pattern
CodeFutures - Java Code Generation
A session bean can access database via JDBC,but it is better not to include the data access code in SessionBean.
It can be implemented in other data access objects.This way your session bean remains independent of data access mechanism.
Agree... missed that one ;)
It's a usual solution we know as "Data Access Object" (aka DAO)
There is no reason your logical DAO cant be a Stateless Session Bean. At least you get the instance pooling advantage.
Of course logically you want to seperate data access from business rules, but dont confuse logical design with physical implementation.
There is no reason your logical DAO cant be a Stateless Session Bean. At least you get the instance pooling advantage.Of course logically you want to seperate data access from business rules, but dont confuse logical design with physical implementation.Dave WolfCynergy SystemsCynergy Systems
It will be interesting to hear from architects who have taken this route. Does the instance pooling offer much of a help in case of DAOs, quantitatively speaking. Would the Romans, Johnsons, Tates, Fowlers and Sumitros approve of this approach?