Skip to content

Spring Jdbc

Zsolt Herpai edited this page Feb 6, 2015 · 2 revisions

Spring provides a transaction-aware datasource proxy to wrap an existing DataSource, to participate seamlessly in Spring transactions: TransactionAwareDataSourceProxy

// wrap a DataSource for transaction management
DataSource dataSource = ...
DataSource transactionalDataSource = new TransactionAwareDataSourceProxy(dataSource);
// create a FluentJdbc ConnectionProvider based on it
ConnectionProvider cp = new DataSourceConnectionProvider(transactionalDataSource);

Alternatively transaction-managed connections can be acquired from JdbcTemplate's Connection callback:

JdbcTemplate jdbcTemplate = ...
ConnectionProvider provider = query -> {
	jdbcTemplate.execute(connection -> {
		query.receive(connection);
   	});
}

Clone this wiki locally