public static void transaction(Runnable action, boolean readOnly) { Ctx ctx = Ctxs.get(); boolean newContext = ctx == null; if (newContext) { ctx = Ctxs.open("transaction"); } try { EntityManager em = ctx.persister(); JPA.with(em).transactional(action, readOnly); } finally { if (newContext) { Ctxs.close(); } } }
@Override public void run() { U.must(!Ctxs.hasContext(), "Detected context leak!"); try { Ctxs.open(context); } catch (Throwable e) { Log.error("Job context initialization failed!", e); throw U.rte("Job context initialization failed!", e); } try { job.run(); } catch (Throwable e) { Log.error("Job execution failed!", e); throw U.rte("Job execution failed!", e); } finally { Ctxs.close(); } }
public static void attach(Ctx ctx) { if (!hasContext()) { if (ctx != null) { CTXS.set(ctx); } } else { throw new IllegalStateException("The context was already opened: " + required()); } }
private void processNextExtraSafe(RapidoidConnection conn) { if (Ctxs.hasContext()) { Log.warn("Detected unclosed context before processing message!"); Ctxs.close(); } try { conn.getProtocol().process(conn); } finally { if (Ctxs.hasContext()) { Log.warn("Detected unclosed context after processing message!"); Ctxs.close(); } } }
public static boolean hasContext() { return get() != null; }
@Override public void run() { U.must(!Ctxs.hasContext(), "Detected context leak!"); try { if (ctx != null) { // U.must(ctx.app() != null, "Application wasn't attached to the context: %s", ctx); Ctxs.attach(ctx); } else { Ctxs.open("job"); Log.debug("Opening new context"); } } catch (Throwable e) { Log.error("Job context initialization failed!", e); throw U.rte("Job context initialization failed!", e); } try { job.run(); } catch (Throwable e) { Log.error("Job execution failed!", e); throw U.rte("Job execution failed!", e); } finally { Ctxs.close(); } }
protected String username() { if (sudo) { return SUPERADMIN; } return asUsername != null ? asUsername : Ctxs.ctx().username(); }
public static void attach(Ctx ctx) { if (!hasContext()) { if (ctx != null) { CTXS.set(ctx); } } else { throw new IllegalStateException("The context was already opened: " + ctx()); } }
public static Req request() { return Ctxs.required().exchange(); }
public static Ctx open(WithContext context) { Ctx ctx = Ctxs.open(context.tag()); ctx.setExchange(context.exchange()); ctx.setPersister(context.persister()); ctx.setUser(context.user()); Coll.assign(ctx.extras(), U.safe(context.extras())); return ctx; }
public static Ctx open(String tag) { Ctx ctx = new Ctx(tag); Log.debug("Opening context", "ctx", ctx); attach(ctx); return ctx; }
private synchronized void clear() { ensureNotClosed(); Log.debug("Clearing context", "ctx", this); this.referenceCounter = 0; this.user = null; this.exchange = null; this.persisters = null; for (Object persister : persistersToClose) { Ctxs.closePersister(this, persister); } persistersToClose.clear(); extras.clear(); closed = true; }
public static boolean hasContext() { return Ctxs.get() != null; }