views:

899

answers:

2
+1  Q: 

EJB 3 with JDBC

Is it possible to use EJB 3 with JDBC. I read somewhere, that it's allowed.

However, I hear that EJB 3 implementation uses JTA, by default. What does that mean for JDBC? Is it only for the transaction support? That means JTA is used for transaction when using JDBC code? Meaning that even local transactions are implemented as global transactions?

Does it mean it's not a good idea to use JDBC with EJB 3? Many people point me to JPA, but it's an ORM. I want to use SQL.

any suggestions?

+1  A: 

You can look at this page, it does appear that you can combine EJB3 with JDBC.

http://www.java2s.com/Tutorial/Java/0415__EJB3/UseJDBCInEJB.htm

James Black
+1  A: 

Hi,

That means JTA is used for transaction when using JDBC code ?

And

Meaning that even local transactions are implemented as global transactions ?

The EJB container CAN MAKE USE of resource manager local transactions AS AN OPTIMIZATION TECHNIQUE for enterprise beans for which distributed transactions ARE NOT NEEDED.

It is a good idea do the following when using a declarative or programmatic transaction demarcation:

  • declare resources using the Resource annotation in the enterprise bean class or using the resource-ref element in the enterprise bean’s deployment descriptor

Something like (setter method or member field)

// mappedName points to a global mapping name
@Resource(mappedName="java:/DefaultDS") 
private javax.sql.DataSource ds;

And inside a business logic method

  • If you are using a declarative transaction

    Connection conn = ds.getConnection();

  • If you are using a programmatic transaction

Declare a setter or member field UserTransaction

@Resource 
private UserTransaction ut;

And

ut.beginTransaction();

Connection conn = ds.getConnection();

ut.commit();

Take care of the following

If you are using a Stateful session bean, do the following in the PrePassivate callback method

  • Close all JDBC connections in the PrePassivate method and assign the instance’s fields storing the connections to null

regards,

Arthur Ronald F D Garcia
good answer, thank you.When you say, "The EJB container CAN MAKE USE of resource manager local transactions AS AN OPTIMIZATION TECHNIQUE for enterprise beans for which distributed transactions ARE NOT NEEDED" does that mean this is done automatically by the EJB 3 container? Is that a JEE standard requirement or a feature offered by some vendors?If it's a feature, then is it configurable or usually done this way, by default?
Shaw
@Shaw Hi, Although IS NOT a requirement, JEE specification ALLOWS it when distributed transaction are not needed. Check your JEE implementation whether it makes use of resource manager local transaction when distributed transaction are are not needed.
Arthur Ronald F D Garcia
And Does that mean this is done automatically by the EJB 3 container ? EJB container can do it AUTOMATICALLY but again it is a good idea check out its documentation.
Arthur Ronald F D Garcia