Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] Applied transaction on multiple sql row update.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Oct 28, 2017
1 parent 21f9ccd commit 6ae652c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class EmbeddedDbSqlRegistry implements UpdatableSqlRegistry {

private JdbcTemplate jdbcTemplate;
private TransactionTemplate transactionTemplate;

public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
}

@Override
Expand Down Expand Up @@ -40,9 +46,14 @@ public void updateSql(String key, String sql) throws SqlUpdateFailureException {
}

@Override
public void updateSql(Map<String, String> sqlMap) throws SqlUpdateFailureException {
for (Map.Entry<String, String> entry : sqlMap.entrySet()) {
updateSql(entry.getKey(), entry.getValue());
}
public void updateSql(final Map<String, String> sqlMap) throws SqlUpdateFailureException {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
for (Map.Entry<String, String> entry : sqlMap.entrySet()) {
updateSql(entry.getKey(), entry.getValue());
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@

public abstract class AbstractUpdatableSqlRegistryTest {

private UpdatableSqlRegistry sqlRegistry;

@Before
public void setUp() {
sqlRegistry = createUpdatableSqlRegistry();
}

protected UpdatableSqlRegistry sqlRegistry;
abstract protected UpdatableSqlRegistry createUpdatableSqlRegistry();

@Before
public void init() {
sqlRegistry = new ConcurrentHashMapSqlRegistry();
sqlRegistry = createUpdatableSqlRegistry();
sqlRegistry.registerSql("KEY1", "SQL1");
sqlRegistry.registerSql("KEY2", "SQL2");
sqlRegistry.registerSql("KEY3", "SQL3");
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/ch07/springbook/EmbeddedDbSqlRegistryTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package ch07.springbook;

import static junit.framework.TestCase.fail;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import ch07.springbook.sql.registry.EmbeddedDbSqlRegistry;
import ch07.springbook.sql.registry.SqlUpdateFailureException;
import ch07.springbook.sql.registry.UpdatableSqlRegistry;

public class EmbeddedDbSqlRegistryTest extends AbstractUpdatableSqlRegistryTest {
Expand All @@ -28,4 +35,22 @@ protected UpdatableSqlRegistry createUpdatableSqlRegistry() {
public void tearDown() {
db.shutdown();
}

@Test
public void transactionalUpdate() {
checkFindResult("SQL1", "SQL2", "SQL3");

Map<String, String> sqlMap = new HashMap<>();
sqlMap.put("KEY1", "Modified1");
sqlMap.put("KEY123!@#", "Modified");

try {
sqlRegistry.updateSql(sqlMap);
fail();
} catch (SqlUpdateFailureException e) {

}

checkFindResult("SQL1", "SQL2", "SQL3");
}
}

0 comments on commit 6ae652c

Please sign in to comment.