aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Using JDBC from SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Using JDBC from SessionBean" Watch "Using JDBC from SessionBean" New topic
Author

Using JDBC from SessionBean

Jeroen de Wolf
Greenhorn

Joined: Jul 31, 2003
Posts: 13
Howdy folks,

Ik have a stateless session bean that uses CMP EJBs to create a reservation. I use the same session bean to do a JDBC read only query (on the same database used for the CMP EJBs) to create a couple of dropdownlists on the web client.

The question is should I do this? or should I use direct JDBC from the web client, to avoid the EJB overhead.

The second question is how to do this. I ran into a problem when I created a JDBC connection in this session EJB. I get a runtime exception when I deploy on WAS 5.0.

"An illegal attempt to enlist a one phase capable resource with existing two-phase capable resources has occurred"

Although I tried different approaches to solve this (using an XA Driver, using two different datasources (one for CMP and one for JDBC), setting the resource reference to unsharable) but nothing worked. I must oversee something in the EJB deployment descriptor or the server configuration.

Can anyone help me out?

IBM WAS 5.0
IBM DB2UDB 8.1


Jeroen de Wolf
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30299
    
150

Jeroen,
Are you trying to access the CMP and JDBC in the same transaction or just in different methods of the same bean? The later should work. The former could create transactional issues depending on the order of the reads.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Phani Kumar
Greenhorn

Joined: Feb 15, 2002
Posts: 22
I vote for putting the other database logic also into EJB. Since you are accessing the CMP bean from a SLSB, the call would effectively become a local call thereby getting rid of overhead involved in a Remote call and Network transit. On the otherhand, if you want to put it outside your CMP beans, go for DAOs and place all such logic inside them. Its always better to keep your database layer seperate from your facade(assuming the SLSB you are using to call CMP is facade)
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
The design principle is that you should never access a database in web-tier. Always do it in EJB-tier only.
I am using stateless session bean and accessing database through DAO. This is the best practice. I suggest that you can use servlet for passing required parameters to the bean which will then call the methods of DAO.
Jeroen de Wolf
Greenhorn

Joined: Jul 31, 2003
Posts: 13
The session and entity beans are not explicitly configured for transactions.
I create a Reservation Entity Bean using the entity beans Car, CarType, RentalStation, etc. and while doing this, I use direct JDBC to query the database for the last known reservation and increase by one.

Is this possible anyway?
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
Hi Joren,
I use the same session bean to do a JDBC read only query (on the same database used for the CMP EJBs) to create a couple of dropdownlists on the web client.
Why don't you use EJB-QL in the CMP for all your JDBC operations?
Jeroen de Wolf
Greenhorn

Joined: Jul 31, 2003
Posts: 13
Hi,

I think I should use EJB QL for that. Yet I am just technically curious if it is possible to do direct JDBC in combination with EJBs and the limitations and consequences (technically) of that. I know there are several other ways to do it properly (by using DAO, EJB QL, etc).

Regards,
Jeroen
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
Think on OR mapping(like Hybernate) also
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JDBC from SessionBean