Created
December 7, 2011 18:32
-
-
Save mdellabitta/1444003 to your computer and use it in GitHub Desktop.
how do do spring jdbc transactions with jdbctemplate/transactiontemplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
} | |
} |
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
Like this simple and concise example! Will put it to the test tomorrow! :)