Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using JDBC from SessionBean

 
Jeroen de Wolf
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Phani Kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think on OR mapping(like Hybernate) also
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic