Last active
August 29, 2015 14:05
-
-
Save elnur/697e7794a94684b09dbe to your computer and use it in GitHub Desktop.
This file contains 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
@Configuration | |
@EnableTransactionManagement | |
public class PersistenceConfig { | |
@Bean | |
public PlatformTransactionManager transactionManager() { | |
return new JpaTransactionManager(entityManagerFactory()); | |
} | |
// other bean definitions | |
} |
This file contains 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 QuestionApiTest { | |
@Autowired | |
private RequestRepository requestRepository; | |
@Autowired | |
private QuestionRepository questionRepository; | |
@Test | |
public void listByRequest() throws Exception { | |
Request request = requestRepository.save(aRequest().build()); | |
Question firstQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
Question secondQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
// Doing an HTTP request to the app to get a list of both questions | |
// found by the given request | |
} | |
} |
This file contains 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 QuestionApiTest { | |
@Autowired | |
private RequestRepository requestRepository; | |
@Autowired | |
private QuestionRepository questionRepository; | |
@Autowired | |
private PlatformTransactionManager transactionManager; | |
@Test | |
public void listByRequest() throws Exception { | |
DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); | |
TransactionStatus transaction = transactionManager.getTransaction(definition); | |
Request request = requestRepository.save(aRequest().build()); | |
Question firstQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
Question secondQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
transactionManager.commit(transaction); | |
// Doing an HTTP request to the app to get a list of both questions | |
// found by the given request | |
} | |
} |
This file contains 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 QuestionApiTest { | |
@Autowired | |
private RequestRepository requestRepository; | |
@Autowired | |
private QuestionRepository questionRepository; | |
@Autowired | |
private Transactor transactor; | |
@Test | |
public void listByRequest() throws Exception { | |
transactor.perform(new UnitOfWork() { | |
@Override | |
public void work() { | |
Request request = requestRepository.save(aRequest().build()); | |
Question firstQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
Question secondQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
} | |
}); | |
// Doing an HTTP request to the app to get a list of both questions | |
// found by the given request | |
} | |
} |
This file contains 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 QuestionApiTest { | |
@Autowired | |
private RequestRepository requestRepository; | |
@Autowired | |
private QuestionRepository questionRepository; | |
@Autowired | |
private Transactor transactor; | |
@Test | |
public void listByRequest() throws Exception { | |
transactor.perform(() -> { | |
Request request = requestRepository.save(aRequest().build()); | |
Question firstQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
Question secondQuestion = questionRepository.save(aQuestion() | |
.withRequest(request) | |
.build()); | |
}); | |
// Doing an HTTP request to the app to get a list of both questions | |
// found by the given request | |
} | |
} |
This file contains 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
@Component | |
public class Transactor { | |
@Autowired | |
private PlatformTransactionManager transactionManager; | |
public void perform(UnitOfWork unitOfWork) { | |
DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); | |
TransactionStatus transaction = transactionManager.getTransaction(definition); | |
try { | |
unitOfWork.work(); | |
transactionManager.commit(transaction); | |
} catch (Exception e) { | |
transactionManager.rollback(transaction); | |
throw e; | |
} | |
} | |
} |
This file contains 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 interface UnitOfWork { | |
void work(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment