Skip to content

Instantly share code, notes, and snippets.

@sebersole
Created November 28, 2011 22:08
Show Gist options
  • Save sebersole/1402311 to your computer and use it in GitHub Desktop.
Save sebersole/1402311 to your computer and use it in GitHub Desktop.
HHH-6780 - Wrong Query timeout calculation
public interface JdbcCoordinator extends Serializable {
/**
* Set the effective transaction timeout period for the current transaction, in seconds.
*
* NOTE : Same signature, just added javadocs...
*/
public void setTransactionTimeOut(int seconds);
/**
* Retrieve the amount of time, in seconds, still remaining before transaction timeout occurs.
*
* NOTE : New method!
*/
public int getRemainingTransactionTimeOutPeriod();
}
public class JdbcCoordinatorImpl implements JdbcCoordinator {
private long transactionTimeOutInstant = -1;
public void setTransactionTimeOut(int seconds) {
// new body!
transactionTimeOutInstant = System.currentTimeMillis() + ( seconds * 1000 );
}
// completely new method, see new StatementPreparerImpl
public int getRemainingTransactionTimeOutPeriod() {
if ( transactionTimeOutInstant < 0 ) {
return -1;
}
final int secondsRemaining = transactionTimeOutInstant - ( System.currentTimeMillis() / 1000 );
if ( secondsRemaining <= 0 ) {
throw new TransactionException( "transaction timeout expired" );
}
return secondsRemaining;
}
public void afterTransaction() {
logicalConnection.afterTransaction();
// changed functionality
transactionTimeOutInstant = -1;
}
}
public interface StatementPreparer {
...
// remove these 2 methods...
- public void setTransactionTimeOut(int timeout);
- public void unsetTransactionTimeOut();
}
class StatementPreparerImpl implements StatementPreparer {
// remove references to local 'transactionTimeOut' variable
private abstract class StatementPreparationTemplate {
...
private void setStatementTimeout(PreparedStatement preparedStatement) throws SQLException {
final int remainingTransactionTimeOutPeriod = jdbcCoordinator.getRemainingTransactionTimeOutPeriod();
if ( remainingTransactionTimeOutPeriod > 0 ) {
preparedStatement.setQueryTimeout( timeout );
}
}
}
}
@sebersole
Copy link
Author

Something like calculateRemainingTransactionTimeOutPeriod() instead of getRemainingTransactionTimeOutPeriod()...

@donnchadh
Copy link

Looks good. This seems much clearer than what is in CR6 and looks like it will solve my issues.

@sebersole
Copy link
Author

sebersole commented Nov 29, 2011 via email

@donnchadh
Copy link

Just downloaded the zip of master and kicked off a gradle build. I'll run our integration tests against it as soon as I can today.

@donnchadh
Copy link

I'm no longer seeing any errors related to the transaction timeout when running our integration tests against master from this morning, so this seems fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment