Skip to content

Instantly share code, notes, and snippets.

@mdellabitta
Created December 7, 2011 18:32
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mdellabitta/1444003 to your computer and use it in GitHub Desktop.
Save mdellabitta/1444003 to your computer and use it in GitHub Desktop.
how do do spring jdbc transactions with jdbctemplate/transactiontemplate
public class ExodusWriter {
private JdbcTemplate jdbcTemplate;
private TransactionTemplate transactionTemplate;
public ExodusWriter(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
jdbcTemplate = new JdbcTemplate(transactionManager.getDataSource());
transactionTemplate = new TransactionTemplate(transactionManager);
}
public void writeImageID(final String imageID, final String uuid) throws Exception {
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
jdbcTemplate.update("delete from image_to_uuid where image_id = ?", imageID);
jdbcTemplate.update("insert into image_to_uuid (image_id, uuid) values (?, ?)", imageID, uuid);
return null;
}
});
}
}
@dimkir
Copy link

dimkir commented Nov 18, 2013

Like this simple and concise example! Will put it to the test tomorrow! :)

@daald
Copy link

daald commented Jun 5, 2018

The best and most compact explanation how to connect JdbcTemplate and TransactionTemplate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment