Name: guice-transactional
Owner: HubSpot
Description: null
Created: 2015-07-23 15:41:32.0
Updated: 2017-05-20 14:14:35.0
Pushed: 2017-04-04 14:06:13.0
Homepage: null
Size: 25
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Miss the convenience of @Transactional
but have no interest in using JPA? Then guice-transactional is for you.
guice-transactional lets you annotate methods using the standard @javax.transaction.Transactional
annotation and works as you
expect; transactions will be started before the method is called and committed or rolled back after the method completes. It
also implements all of the
transaction types for
more complicated use-cases.
guice-transactional just needs access to the underlying javax.sql.DataSource
so it will work with any persistence framework,
including jDBI, Hibernate, MyBatis, QueryRunner, or even raw JDBC.
To use on Maven-based projects, add the following dependency:
endency>
roupId>com.hubspot.guice</groupId>
rtifactId>guice-transactional</artifactId>
ersion>0.2.3</version>
pendency>
There are a few steps to get up and running with guice-transactional:
Install TransactionalModule
when building your Guice injector, this sets up the necessary method interceptor
When configuring your persistence framework, wrap your DataSource
in a TransactionalDataSource
Annotate methods with @javax.transaction.Transactional
and profit! The only restriction is that these methods must be on
objects that were created by Guice and they must not have private
access
When you install TransactionalModule
, an interceptor is added to methods annotated with @javax.transaction.Transactional
.
When a transactional method is entered, the interceptor stores this state in a ThreadLocal
. The TransactionalDataSource
checks
the interceptor for this flag when a connection is requested. If the flag is set, it disables auto-commit and returns the same
connection for the duration of the transactional method. Once the transactional method completes, the interceptor either commits or
rolls back the transaction depending on the result of the method call and then clears the thread local state.