Skip to content

Transactions

Zsolt Herpai edited this page Sep 28, 2015 · 13 revisions
Programmatic transaction handling
query.transaction().in(
	() -> {
		query
        	.update("UPDATE CUSTOMER SET NAME = ?, ADDRESS = ?")
        	.params("John Doe", "Dallas")
        	.run();
		// ... other queries, method calls, etc..
	}
)

Any queries executed by FluentJdbc within this callback will join this transaction - as long as they're in the same thread, and based on the same ConnectionProvider (DataSource). Connections / transactions to multiple DataSources - through multiple FluentJdbc setups/instances - are supported.

Using a transaction-aware DataSource

Many frameworks provide transaction-aware DataSources. If's ConnectionProvider is based on such a DataSource, all FluentJdbc operations will be executed as part of the current transaction (if any). See integration pages for concrete examples:

Using the underlying connections of JPA

The underlying connection of a JPA session can be extracted from the EntityManager. See integration pages for detailed examples:

Transaction-managed Connection callback

Some frameworks provide transaction-management through Connection callbacks. The API of ConnectionProvider makes these easy to integrate with. See integration page for examples:

Standalone transaction-management with GuicePersist (non-JPA)

Guice Persist transaction-management / annotations can be used without JPA. FluentJdbc Guice Persist extension library provides a module for this:

Custom ConnectionProvider

Custom ConnectionProvider can be implemented to integrate with other transaction-management solutions.