/** * Set the timeout for this object in seconds. * @param seconds number of seconds until expiration */ publicvoidsetTimeoutInSeconds(int seconds) { setTimeoutInMillis(seconds * 1000); }
/** * Set the timeout for this object in milliseconds. * @param millis number of milliseconds until expiration */ publicvoidsetTimeoutInMillis(long millis) { this.deadline = newDate(System.currentTimeMillis() + millis); }
/** * Return the time to live for this object in seconds. * Rounds up eagerly, e.g. 9.00001 still to 10. * @return number of seconds until expiration * @throws TransactionTimedOutException if the deadline has already been reached */ publicintgetTimeToLiveInSeconds() { doublediff= ((double) getTimeToLiveInMillis()) / 1000; intsecs= (int) Math.ceil(diff); checkTransactionTimeout(secs <= 0); return secs; }
/** * Return the time to live for this object in milliseconds. * @return number of millseconds until expiration * @throws TransactionTimedOutException if the deadline has already been reached */ publiclonggetTimeToLiveInMillis()throws TransactionTimedOutException{ if (this.deadline == null) { thrownewIllegalStateException("No timeout specified for this resource holder"); } longtimeToLive=this.deadline.getTime() - System.currentTimeMillis(); checkTransactionTimeout(timeToLive <= 0); return timeToLive; }
/** * Set the transaction rollback-only if the deadline has been reached, * and throw a TransactionTimedOutException. */ privatevoidcheckTransactionTimeout(boolean deadlineReached)throws TransactionTimedOutException { if (deadlineReached) { setRollbackOnly(); thrownewTransactionTimedOutException("Transaction timed out: deadline was " + this.deadline); } }
/** * Apply the specified timeout - overridden by the current transaction timeout, * if any - to the given JDBC Statement object. * @param stmt the JDBC Statement object * @param dataSource the DataSource that the Connection was obtained from * @param timeout the timeout to apply (or 0 for no timeout outside of a transaction) * @throws SQLException if thrown by JDBC methods * @see java.sql.Statement#setQueryTimeout */ publicstaticvoidapplyTimeout(Statement stmt, DataSource dataSource, int timeout)throws SQLException { Assert.notNull(stmt, "No Statement specified"); Assert.notNull(dataSource, "No DataSource specified"); ConnectionHolderholder= (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (holder != null && holder.hasTimeout()) { // Remaining transaction timeout overrides specified value. stmt.setQueryTimeout(holder.getTimeToLiveInSeconds()); } elseif (timeout >= 0) { // No current transaction timeout -> apply specified value. stmt.setQueryTimeout(timeout); } }