Sanjay,
1)
For your statement :
[b]"resource.getResourceState().isFree() will never be free for the ResourceHandle stored in the J2EETransaction object"[/b]
following scenario will return [u]true[/u] for "isNonXAResourceAndFree() "
----------------------------------------------------------------------------------------------------------------
UserTransaction uTx = entityContext.getUserTransaction();
uTx.begin();
con1 = dataSource.getConnection();
doDBOperation() using connection-1
con1.close();
con2 = dataSource.getConnection();
doDBOperation() using connection-2
uTx.commit();
con2.close();
----------------------------------------------------------------------------------------------------------------
2)
I assume your question is why a connection holder is created (with physical connection) for a local transaction and then associated to the physical connection that is already present in transaction.
The reason is, scope (usage of) connections may be beyond a transaction. Application may get a connection within a transaction, complete the transaction, do not close it and use it for some other purpose.
In your snippet : (nested getConnection())
-----------------------------------------------------------------------------
Ejb1.method() {
con1 = dataSource.getConnection("jdbc/dbconn");
doSomething();
// could be another method in same bean or another EJB with tx marked REQUIRED, hence that call will run in the same transaction that this method is running in.
method2();
con1.close();
}
[Ejb2 or Ejb1].method2() {
con2 = dataSource.getConnection("jdbc/dbconn");
doSomething();
con2.close();
}
-----------------------------------------------------------------------------
[b]There is still a necessity to share connections and map it back once transaction is completed.[/b]
Changing your code snippet for inner most EJB call :
EJB2 {
Connection cachedConnection;
[Ejb2].method2() {
con2 = dataSource.getConnection("jdbc/dbconn");
doSomething();
/* con2.close(); */ [i] do not close the connection, instead store it in a global variable[/i]
cachedConnection = con2;
}
[i]//make separate call to method3() once the nested Tx of method2 is complete.[/i]
EJB2.method3(){
use cachedConnection to do db operations;
}
}
Now without sharing (mapping) and re-association during transaction completion, it is not possible to serve method3().
GlassFish is not leaking/wasting resources.
HTH
-Jagadish
[Message sent by forum member 'jr158900' (jr158900)]
http://forums.java.net/jive/thread.jspa?messageID=224408