Skip to content

Instantly share code, notes, and snippets.

@elnur
Last active August 29, 2015 14:05
Show Gist options
  • Save elnur/697e7794a94684b09dbe to your computer and use it in GitHub Desktop.
Save elnur/697e7794a94684b09dbe to your computer and use it in GitHub Desktop.
@Configuration
@EnableTransactionManagement
public class PersistenceConfig {
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
// other bean definitions
}
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
}
}
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
}
}
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
}
}
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
}
}
@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;
}
}
}
public interface UnitOfWork {
void work();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment