db-transaction-manager

Simply DB transaction manager

License

License

Categories

Categories

Net
GroupId

GroupId

net.moznion
ArtifactId

ArtifactId

db-transaction-manager
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

db-transaction-manager
Simply DB transaction manager
Project URL

Project URL

https://github.com/moznion/java-db-transaction-manager
Source Code Management

Source Code Management

https://github.com/moznion/java-db-transaction-manager

Download db-transaction-manager

How to add to project

<!-- https://jarcasting.com/artifacts/net.moznion/db-transaction-manager/ -->
<dependency>
    <groupId>net.moznion</groupId>
    <artifactId>db-transaction-manager</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.moznion/db-transaction-manager/
implementation 'net.moznion:db-transaction-manager:1.1.0'
// https://jarcasting.com/artifacts/net.moznion/db-transaction-manager/
implementation ("net.moznion:db-transaction-manager:1.1.0")
'net.moznion:db-transaction-manager:jar:1.1.0'
<dependency org="net.moznion" name="db-transaction-manager" rev="1.1.0">
  <artifact name="db-transaction-manager" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.moznion', module='db-transaction-manager', version='1.1.0')
)
libraryDependencies += "net.moznion" % "db-transaction-manager" % "1.1.0"
[net.moznion/db-transaction-manager "1.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.10

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.14.8

test (5)

Group / Artifact Type Version
org.slf4j : slf4j-simple jar 1.7.10
junit : junit jar 4.11
com.google.code.findbugs : findbugs jar 3.0.0
mysql : mysql-connector-java jar 5.1.34
net.moznion : capture-output-stream jar 1.0.0

Project Modules

There are no modules declared in this project.

db-transaction-manager

Build Status Maven Central javadoc.io

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;

javadoc.io

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.

Versions

Version
1.1.0
1.0.1
1.0.0