EJB3 DataSource DataSource.getConnection

In CMT J2EE environement (container managed transaction), what transaction / connection is used when I JDNI lookup the DataSource object and call DataSource.getConnection

?

Is this relationship part of a (potentially distributed) transaction? Does it return getConnection()

the same connection every time I call it on the same DataSource object? I only know of using Connections by the same EntityManager using native SQL statements.

This is what puzzles me. As I understand it, the SessionContext defines the transactional system that is used every time I use the datasource. The problem I have is that inside a session a bean is used DataSource.getConnection()

and that connection is then closed. If there is a problem, it is issued SessionContext.setForRollBack(true)

.

So how does the transactional service context relate to the DataSource?

If we create a new connection every time the datasource is used, or at least viewed, I'm having trouble understanding what I already know. Any clarification would be great. I know container managed transaction and other systems, but the actual behavior of the DataSource is completely outside of me.

+3


source to share


2 answers


In Java EE, a transaction is a concept that is not exclusive to databases, for example, JMS sessions can also be part of a container-managed transaction. The idea is that one or more methods run under the container-managed transaction boundary, the container will commit or rollback the transaction as needed.

There are multiple layers in the data source associated with the database, fist is the managed connection pool that is maintained by the container, the second is the actual management of the database driver's connection to the database, in Java, the connection is an abstraction for a session with the database, not with the physical connection managed by the driver.

In the above context, your questions can be resolved:

what transaction / connection is used when I JDNI search for a DataSource object and DataSource.getConnection is called.

In a container managed transaction, although it is implementation dependent, there is a connection / session associated with a database that is marked with a transaction boundary. The actual physical connection can be shared with the database using the driver, but this is transparent to the application as well as the container.



Is this relationship part of a (potentially distributed) transaction? Does getConnection get the same connection every time I call it on the same DataSource object?

See above, the connection has nothing to do with the underlying database socket opened by the driver. It is a logically separate session, and if, within a transaction boundary, the same session is associated with a connection obtained from a data source, then how this is implemented is part of the container design.

The problem I have is that inside a session bean the DataSource.getConnection is used and this connection is then closed

In the pooled implementation for the connection, Connection.close () has no effect, the connection is returned to the pool ( http://commons.apache.org/proper/commons-dbcp/api-1.3/org/apache/commons/dbcp/PoolableConnection.html ) , this behavior is the same for all pools. Thus, binding a connection does not necessarily detach it from the container's transaction boundary, although the connection should not be closed in a container-managed transaction. Likewise, commit, setAutoCommit, rollback should not be called from the CMT, as this will issue the next command, equivalent to the actual database, and the transaction behavior after that will be undefined.

+4


source


Take a look at the @Resource annotation declaration .

It includes a shareable attribute that lets you specify the sharing behavior. The default is used true

, which means that you will automatically access the connection if you do nothing.



This attribute is also included in the XML Schema for any resource-ref

specific data sources that you can search using JNDI.

All resources (JDBC, JMS, ResourceManager) that are included at the time of the call are registered with the current transaction. Sometimes you may need to specify the use of XA to work correctly.

0


source







All Articles