Skip to content

Instantly share code, notes, and snippets.

@atamanroman
Last active October 1, 2015 23:58
Show Gist options
  • Save atamanroman/2128169 to your computer and use it in GitHub Desktop.
Save atamanroman/2128169 to your computer and use it in GitHub Desktop.
Broken use of REQUIRES_NEW
@Singleton
public class Importer {
@EJBContext
private EJBContext ctx;
private ConcurrentLinkedQueue queue = new ...
private AtomicBoolean isRunning = false;
public void add(ImportItem itm) {
queue.offer(item);
if(!isRunning) start();
}
public void start() {
while(!queue.isEmpty())
this.importItem(queue.poll());
}
@TransactionAttribute(REQUIRES_NEW)
public void importItem() {
try {
// do some fancy xml stuff
} catch (Exception ex) {
ctx.setRollbackOnly();
}
}
}
@EJB
private Importer thisBean;
public void start()
{
while(!queue.isEmpty())
thisBean.importItem(queue.poll());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment