Spring’s DataSourceUtils defines a method that applies a transaction timeout, without specifying it explicitly. The Javadoc reads:
public static void applyTransactionTimeout(Statement stmt,
DataSource dataSource)
throws SQLException
Apply the current transaction timeout, if any, to the given JDBC Statement object.
My questions are:
- Where does the timeout value come from?
- Why is the call needed at all? If the transaction is acquired through Spring, wouldn’t whatever timeout (or the default one) be applied anyway?
Thank you.
Value comes from the current transaction, if any (i.e.
@Transactional(..., timeout = ...)).Spring’s
DataSourceUtils.getConnection()doesn’t proxy connections it return, therefore Spring cannot apply any configuration toStatements created by these connections automatically.Actually, the recommended way to use JDBC in Spring is to do it through
JdbcTemplate.JdbcTemplateis fully transaction-aware, therefore it calls the method in question internally if needed.But if you need to use raw JDBC
Connections, you can use this method to configure them according to@Transactional(...)and so on.