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

Commit

Permalink
[ch05] Use TransactionSynchronizationManager for DB transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 6, 2017
1 parent e1eed87 commit 1dda439
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
40 changes: 34 additions & 6 deletions src/main/java/ch01/springbook/user/UserService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package ch01.springbook.user;

import java.sql.Connection;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import ch01.springbook.user.dao.UserDao;
import ch01.springbook.user.domain.Level;
import ch01.springbook.user.domain.User;
Expand All @@ -12,20 +18,42 @@ public class UserService {
public static final int MIN_RECOMMEND_FOR_GOLD = 30;

UserDao userDao;
private DataSource dataSource;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public void upgradeLevels() {
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void upgradeLevels() throws Exception {

List<User> userList = userDao.getAll();
TransactionSynchronizationManager.initSynchronization();
Connection c = DataSourceUtils.getConnection(dataSource);
c.setAutoCommit(false);

for (User user : userList) {
try {
List<User> userList = userDao.getAll();

if (canUpgradeLevel(user) == true) {
upgradeLevel(user);
for (User user : userList) {

if (canUpgradeLevel(user) == true) {
upgradeLevel(user);
}
}

c.commit();

} catch (Exception e) {
c.rollback();
throw e;

} finally {
DataSourceUtils.releaseConnection(c, dataSource);
TransactionSynchronizationManager.unbindResource(this.dataSource);
TransactionSynchronizationManager.clearSynchronization();
}
}

Expand All @@ -49,7 +77,7 @@ private boolean canUpgradeLevel(User user) {
}
}

private void upgradeLevel(User user) {
protected void upgradeLevel(User user) {
user.upgradeLevel();
userDao.update(user);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<bean id="userService" class="ch01.springbook.user.UserService">
<property name="userDao" ref="userDao" />
<property name="dataSource" ref="dataSource" />
</bean>

</beans>
49 changes: 48 additions & 1 deletion src/test/java/ch01/springbook/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -24,12 +27,35 @@
@ContextConfiguration(locations = "/applicationContext.xml")
public class UserServiceTest {

static class TestUserServiceException extends RuntimeException {

}

static class TestUserService extends UserService {
private String id;

private TestUserService(String id) {
this.id = id;
}

protected void upgradeLevel(User user) {
if (user.getId().equals(this.id)) {
throw new TestUserServiceException();
}

super.upgradeLevel(user);
}
}

@Autowired
UserService userService;

@Autowired
UserDao userDao;

@Autowired
DataSource dataSource;

List<User> userList;

@Before
Expand All @@ -50,7 +76,7 @@ public void bean() {
}

@Test
public void upgradeLevels() {
public void upgradeLevels() throws Exception {

userDao.deleteAll();

Expand Down Expand Up @@ -86,6 +112,27 @@ public void add() {
assertThat(userWithoutLevelRead.getLevel(), is(Level.BASIC));
}

@Test
public void upgradeAllOrNothing() throws Exception {
UserService testUserService = new TestUserService(userList.get(3).getId());
testUserService.setUserDao(userDao);
testUserService.setDataSource(dataSource);

userDao.deleteAll();
for (User user : userList) {
userDao.add(user);
}

try {
testUserService.upgradeLevels();
fail("TestUserServiceException expected.");
} catch (TestUserServiceException e) {

}

checkLevelUpgraded(userList.get(1), false);
}

private void checkLevelUpgraded(User user, boolean upgraded) {

User userUpdate = userDao.get(user.getId());
Expand Down

0 comments on commit 1dda439

Please sign in to comment.