Last active
August 29, 2015 14:25
-
-
Save maxant/452fa4b4a181e4b59dee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@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