db-transaction-manager
Simply DB transaction manager for Java.
Synopsis
Basic transaction with commit
TransactionManager txnManager = new TransactionManager(connection);
txnManager.txnBegin();
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO foo (id, var) VALUES (1, 'baz')")) {
preparedStatement.executeUpdate();
}
txnManager.txnCommit(); // insert successfully
Basic transaction with rollback
TransactionManager txnManager = new TransactionManager(connection);
txnManager.txnBegin();
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO foo (id, var) VALUES (1, 'baz')")) {
preparedStatement.executeUpdate();
}
txnManager.txnRollback(); // rollback
Scope based transaction with commit
TransactionManager txnManager = new TransactionManager(connection);
try (TransactionScope txn = new TransactionScope(txnManager)) {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO foo (id, var) VALUES (1, 'baz')")) {
preparedStatement.executeUpdate();
}
txn.commit(); // insert successfully
}
Scope based transaction with rollback
TransactionManager txnManager = new TransactionManager(connection);
try (TransactionScope txn = new TransactionScope(txnManager)) {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO foo (id, var) VALUES (1, 'baz')")) {
preparedStatement.executeUpdate();
}
txn.rollback(); // rollback
}
Scope based transaction with implicit rollback
TransactionManager txnManager = new TransactionManager(connection);
try (TransactionScope txn = new TransactionScope(txnManager)) {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO foo (id, var) VALUES (1, 'baz')")) {
preparedStatement.executeUpdate();
}
} // if reach here without any action (commit or rollback), transaction will rollback automatically
Description
transaction-manager is a simply DB transaction manager.
This package provides begin
, commit
, rollback
and addEndHook
function for transaction. And also provides scope based transaction manager (scope based means it is with try-with-resources
statement).
This package is inspired by DBIx::TransactionManager from Perl.
Please refer to the javadoc for details;
Behavior of Nested Transactions
If any of nested transaction is rollbacked, all of the transactions will rollback.
Dependencies
- Java 8 or later
See Also
Author
moznion ([email protected])
License
The MIT License (MIT)
Copyright © 2014 moznion, http://moznion.net/ <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.