Pub/Sub 모델이란?
EntityManagerFactory
EntityManager
public void createMember() {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
Member member = new Member("name", "age");
em.persist(member);
transaction.commit();
}
XML 설정을 이용한 분리
public class UserDaoJdbc implements UserDao {
private Map<String, String> sqlMap;//UserDAO DI 할때 Property 값으로 주입
public void setSqlMap(Map<String, String> sqlMap) {
this.sqlMap = sqlMap;
}
}
import org.springframework.core.io.Resource
[1] 비즈니스 로직과 트랜잭션 경계설정의 분리
public void upgradeLevels() throws SQLException {
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
upgradeLevelsInternal();
transactionManager.commit(status);
} catch (RuntimeException e) {
transactionManager.rollback(status);
throw e;
}
}
private void upgradeLevelsInternal() {
List<User> users = userDao.getAll();
for (User user : users) {
if (canUpgradeLevel(user)) {
upgradeLevel(user);
}
}
}