A toolset for Jdbc
There is some tools:
We give some wrappers to easily decorate DataSource
and Connection
.
public final MyDataSource extends DataSourceWrap {
public MyDataSource(final DataSource origin) {
super(origin);
}
}
public final MyConnection extends ConnectionWrap {
public MyConnection(final Connection origin) {
super(origin)
}
}
Sometimes, you don't want some pieces of code to close your connection after use. So, to prevent
them to close your connection, you can decorate it with LockedConnection
before give them
like this:
new LockedConnection(
connection
)
Sometimes, you are in situations where you want to use only one connection during the current thread
and be sure that all modifications are taken into account only when you decide to explicitly commit
them. Then, LocalLockedDataSource
is your friend in such case. Just decorate your datasource like
this :
try (final Connection connection = datasource.getConnection()) {
final DataSource uds = new LocalLockedDataSource(
datasource, connection
);
final Connection conn1 = uds.getConnection();
// We do here some operations with our connection.
// After that, we attempt to commit and close it.
conn1.commit(); // no effect
conn1.close(); // no effect
...
// Somewhere else in the same thread, we want a connection
// to do another operations.
final Connection conn2 = uds.getConnection(); // we get here the current connection
...
...
// We choose now to commit all changes.
// For that, we should use connection that is stored in the local thread `cthread`.
cthread.get().commit();
};
If you're using Maven, you should add it to your pom.xml
dependencies like this:
<dependency>
<groupId>com.baudoliver7</groupId>
<artifactId>jdbc-toolset</artifactId>
<version><!-- latest version --></version>
</dependency>
Fork repository, make changes, send us a pull request. We will review
your changes and apply them to the master
branch shortly, provided
they don't violate our quality standards. To avoid frustration, before
sending us your pull request please run full Maven build:
mvn clean install -Pqulice
Keep in mind that JDK 8 and Maven 3.1.0 are the lowest versions you may use.
If you have questions or general suggestions, don't hesitate to submit a new Github issue.