Where to write SQL query in a Stateless Session Bean?
Hi
I want to query a database table from a Stateless Session Bean (SSB). I have the getter and setter methods. And am connecting to the database in ejbCreate and disconnecting in ejbRemove methods. But where should I query the database to retrieve the data? In case of a entity bean it is written in the ejbLoad but where in SSB.
Thanks in advance
Siva
Stateless Session Bean as its name indicates, it has no state, ie it should have no member variables. In another word, SLSB is method based.
Hence, if somehow you need to access to the database in one of the method, you have to open db connection and query the table in that method.
BTW, a Stateless Session Bean should not have getter and setter methods, because it should not keep any stateful information.
--lichu
Stateless session beans (SLSB's) can contain member variables, they just aren't involved with client sessions the was stateful session bean state is. It is popular to create a data access object (DAO) which is some other Java class that your SLSB can use to access the database. This class has methods which get a DB connection from the pool, execute the query and return the appropriate result. The SLSB calls the DAO method(s) from a business method (not one of the ejb methods).
This serves as a decoupling of the delegation of accessing the database with the business process involved with the client.
Hi swatdbaCan you please tell me why a factory, interface and implementation are used in DAO. For example in pet store, there are OraderDAO, OrderDAOFactory and OrderDAOImpl. Can't we write just one class which can operate on the database.Thanks Siva