Performs the closure within a transaction using a cached connection. If the closure takes a single argument, it will be called with the connection, otherwise it will be called with no arguments. @param closure the given closure @throws SQLException if a database error occurs
(@ClosureParams(value=SimpleType.class, options="java.sql.Connection") Closure closure)
| 4079 | * @throws SQLException if a database error occurs |
| 4080 | */ |
| 4081 | public void withTransaction(@ClosureParams(value=SimpleType.class, options="java.sql.Connection") Closure closure) throws SQLException { |
| 4082 | boolean savedCacheConnection = cacheConnection; |
| 4083 | cacheConnection = true; |
| 4084 | Connection connection = null; |
| 4085 | boolean savedAutoCommit = true; |
| 4086 | try { |
| 4087 | connection = createConnection(); |
| 4088 | savedAutoCommit = connection.getAutoCommit(); |
| 4089 | connection.setAutoCommit(false); |
| 4090 | callClosurePossiblyWithConnection(closure, connection); |
| 4091 | connection.commit(); |
| 4092 | } catch (SQLException | Error | RuntimeException e) { |
| 4093 | handleError(connection, e); |
| 4094 | throw e; |
| 4095 | } catch (Exception e) { |
| 4096 | handleError(connection, e); |
| 4097 | throw new SQLException("Unexpected exception during transaction", e); |
| 4098 | } finally { |
| 4099 | if (connection != null) { |
| 4100 | try { |
| 4101 | connection.setAutoCommit(savedAutoCommit); |
| 4102 | } |
| 4103 | catch (SQLException e) { |
| 4104 | LOG.finest("Caught exception resetting auto commit: " + e.getMessage() + " - continuing"); |
| 4105 | } |
| 4106 | } |
| 4107 | cacheConnection = false; |
| 4108 | closeResources(connection, null); |
| 4109 | cacheConnection = savedCacheConnection; |
| 4110 | if (dataSource != null && !cacheConnection) { |
| 4111 | useConnection = null; |
| 4112 | } |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | /** |
| 4117 | * Returns true if the current Sql object is currently executing a withBatch |
nothing calls this directly
no test coverage detected