Skip to content

Instantly share code, notes, and snippets.

@maxant
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxant/452fa4b4a181e4b59dee to your computer and use it in GitHub Desktop.
Save maxant/452fa4b4a181e4b59dee to your computer and use it in GitHub Desktop.
@Stateless
public class Service5 {
@Resource ManagedExecutorService mes;
@Resource EJBContext ctx;
@PersistenceContext(name="asdf") EntityManager em;
@Asynchronous
public void foo(CompletableFuture<String> cf, final PrintWriter pw) {
//pw.write("<br>inside the service we can rollback, i.e. we have access to the transaction");
//ctx.setRollbackOnly();
//in EJB we can use EM
KeyValuePair kvp = new KeyValuePair("asdf");
em.persist(kvp);
Future<String> f = mes.submit(new Callable<String>() {
@Override
public String call() throws Exception {
try{
ctx.setRollbackOnly();
pw.write("<br/>inside executor service, we can rollback the transaction");
}catch(Exception e){
pw.write("<br/>inside executor service, we CANNOT rollback the transaction: " + e.getMessage());
}
try{
//in task inside executor service we CANNOT use EM
KeyValuePair kvp = new KeyValuePair("asdf");
em.persist(kvp);
pw.write("...inside executor service, we can use the EM");
}catch(TransactionRequiredException e){
pw.write("...inside executor service, we CANNOT use the EM: " + e.getMessage());
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment