EJB 3 with JDBC

Is it possible to use EJB 3 with JDBC. I read somewhere that this is allowed.

However, I heard that the EJB 3 implementation uses JTA by default. What does this mean for JDBC? Is it just for transaction support? Does this mean that JTA is used for transaction when using JDBC code? Does this mean that even local transactions are implemented as global transactions?

Does this mean that it is not recommended to use JDBC with EJB 3? Many people point me to JPA, but this is ORM. I want to use SQL.

any suggestions?

+3
source share
3 answers

Does this mean that JTA is used for transaction when using JDBC code?

and

, ?

EJB beans, .

:

  • , Resource enterprise bean ref-ref bean

- ( setter )

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

-

  • conn = ds.getConnection();

UserTransaction

@Resource 
private UserTransaction ut;

ut.beginTransaction();

Connection conn = ds.getConnection();

ut.commit();

stateful bean, PrePassivate

  • JDBC PrePassivate , , null

,

+2

JPA2, entityManager.unwrap(Connection.class) JDBC.

:

Connection connection = entityManager.unwrap( Connection.class );
try (Statement stmt = connection.createStatement()) {
    stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
}

, JDBC EJB 3? JPA, ORM. SQL.

This was once necessary for performance or compatibility issues. I usually use this method to execute PL / PSQL with array parameters, complex Posgis SQL, etc.

+1
source

Source: https://habr.com/ru/post/1718621/


All Articles