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

Commit

Permalink
[ch05] UserServiceTx using dynamic proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 9, 2017
1 parent bc3b6cd commit 249bba7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/ch01/springbook/user/TransactionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch01.springbook.user;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class TransactionHandler implements InvocationHandler {

private Object target;
private PlatformTransactionManager transactionManager;
private String pattern;

public void setTarget(Object target) {
this.target = target;
}

public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

public void setPattern(String pattern) {
this.pattern = pattern;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

if (method.getName().startsWith(pattern)) {
return invokeInTransaction(method, args);
} else {
return method.invoke(target, args);
}
}

private Object invokeInTransaction(Method method, Object[] args) throws Throwable {

TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());

try {
Object ret = method.invoke(target, args);
transactionManager.commit(status);
return ret;

} catch (InvocationTargetException e) {
transactionManager.rollback(status);
throw e.getTargetException();
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/ch01/springbook/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -135,6 +136,35 @@ public void upgradeAllOrNothing() throws Exception {
checkLevelUpgraded(userList.get(1), false);
}

@Test
public void upgradeAllOrNothingUsingDynamicProxy() throws Exception {

TestUserService testUserService = new TestUserService(userList.get(3).getId());
testUserService.setUserDao(userDao);

TransactionHandler txHandler = new TransactionHandler();
txHandler.setTarget(testUserService);
txHandler.setTransactionManager(transactionManager);
txHandler.setPattern("upgradeLevels");

UserService txUserService = (UserService)Proxy.newProxyInstance(
getClass().getClassLoader(), new Class[] { UserService.class }, txHandler);

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

try {
txUserService.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 249bba7

Please sign in to comment.