PDA

View Full Version : JdbcCursorInputSource and transaction



gpitteloud
Jan 14th, 2008, 06:11 AM
As I walk through source code for JdbcCursorInputSource, I notice that the executeQuery() method get a SqlConnection by just calling DataSource.getConnection(). Shouldn't it be something like DataSourceUtils.getconnection(DataSource ds), as we might want to get the connection from the current transaction ?

Dave Syer
Jan 14th, 2008, 08:47 AM
Actually you almost certainly do not want the connection from the current transaction. The cursor has to stay open over the whole step execution (the duration of many transactions), so we have to explicitly manage the connection indepdendently of DataSourceUtils.

lucasward
Jan 14th, 2008, 09:30 AM
The other reason that the DataSourceUtils isn't used is because it will close a connection after a transaction completes:



public void afterCompletion(int status) {
// If we haven't closed the Connection in beforeCompletion,
// close it now. The holder might have been used for other
// cleanup in the meantime, for example by a Hibernate Session.
if (this.holderActive) {
// The thread-bound ConnectionHolder might not be available anymore,
// since afterCompletion might get called from a different thread.
if (TransactionSynchronizationManager.hasResource(thi s.dataSource)) {
TransactionSynchronizationManager.unbindResource(t his.dataSource);
}
this.holderActive = false;
if (this.connectionHolder.hasConnection()) {
releaseConnection(this.connectionHolder.getConnect ion(), this.dataSource);
// Reset the ConnectionHolder: It might remain bound to the thread.
this.connectionHolder.setConnection(null);
}
this.connectionHolder.reset();
}
}


See the TransactionSyncrhonization method for more information on afterCompletion.