/** * Open underlying Hibernate session. * @throws HibernateException */ public synchronized Session open () throws HibernateException { return db.open(); } /**
public static <T> T exec(String configModifier, DBAction<T> action) throws Exception { try (DB db = new DB(configModifier)) { db.open(); return action.exec(db); } }
public static <T> T exec(DBAction<T> action) throws Exception { try (DB db = new DB()) { db.open(); return action.exec(db); } }
private void ensureOpen() { if (db == null) { db = new DB(); db.open(); mgr = new SysConfigManager(db); } } private void close() {
private synchronized void touch (String id, String detail) { int sp = detail.indexOf (" "); String state = Status.OFF; if (sp > 0 && detail.length() > sp) { state = detail.substring (0, sp); detail = detail.substring (++sp); } try { db.open (); mgr.touch (id, state, detail); } catch (Throwable t) { getLog().error (t); } finally { close(); } } private synchronized String getNextCommand (String id) {
@Override public void exec(CLIContext cli, String[] args) throws Exception { String filename = "-"; // defaults to stdout ' String configModifier = null; boolean create = false; if (args.length > 1) filename = args[1]; if (args.length > 2) create = "yes".equalsIgnoreCase(args[2]) || "true".equalsIgnoreCase(args[2]); if (args.length > 3) configModifier = args[3]; try (DB db = new DB(configModifier)) { db.open(); db.beginTransaction(); db.createSchema(filename, create); } } }
private void create (String id) { try (DB db = new DB()) { db.open(); db.beginTransaction(); SeqNo seq = new SeqNo(id); db.session().save(seq); db.commit(); } catch (Exception ignored) { } } }
private synchronized void setResponse (String id, String response) { try { db.open (); mgr.setResponse (id, response); } catch (Throwable t) { getLog().error (t); } finally { close(); } } public class MonitorTimerTask extends TimerTask {
public static <T> T execWithTransaction(DBAction<T> action) throws Exception { try (DB db = new DB()) { db.open(); db.beginTransaction(); T obj = action.exec(db); db.commit(); return obj; } }
private synchronized String getNextCommand (String id) { try { db.open (); return mgr.getNextCommand (id); } catch (Throwable t) { getLog().error (t); } finally { close(); } return null; } private synchronized void setResponse (String id, String response) {
public static <T> T execWithTransaction(String configModifier, DBAction<T> action) throws Exception { try (DB db = new DB(configModifier)) { db.open(); db.beginTransaction(); T obj = action.exec(db); db.commit(); return obj; } }
private void createSchema () throws HibernateException, DocumentException { DB db = new DB(); db.open(); db.beginTransaction(); db.createSchema(null, true); db.commit(); db.close(); }
@Override public void exec(CLIContext cli, String[] args) throws Exception { try (DB db = new DB()) { db.open(); RoleManager rm = new RoleManager(db); for (Role r : rm.getAll()) { cli.println (r.getName() + " " + r.getPermissions()); } } catch (Exception e) { cli.println (e.getMessage()); } } }
@Override public void exec(CLIContext cli, String[] args) throws Exception { try (DB db = new DB()) { db.open(); SysConfigManager mgr = new SysConfigManager(db); if (cli.getUserData().containsKey(SYSCONFIG.PREFIX)) mgr.setPrefix((String) cli.getUserData().get(SYSCONFIG.PREFIX)); else if (args.length > 1) mgr.setPrefix(args[1]); String format = "%" + mgr.getMaxIdLength() + "s: %s"; for (SysConfig sc : mgr.getAll()) { cli.println (String.format (format, sc.getId(), sc.getValue())); } } catch (Exception e) { cli.println (e.getMessage()); } } }
public void run() { long start = System.currentTimeMillis(); for (int i=1; running(); i++) { try { db.open (); if (cfg.getBoolean ("check", true)) mgr.check (); mgr.touch (statusId, Status.OK, getDetail (start, i)); Thread.sleep (interval); } catch (Throwable t) { getLog().error (t); ISOUtil.sleep (1000); } finally { close(); } } } private void close() {
public void testAddExportUser() throws Exception { Session sess = new DB().open(); try { GLUser user = getUser(sess,System.getProperty("user.name")); } catch (IllegalArgumentException e) { Transaction txn = sess.beginTransaction(); GLUser user = new GLUser(); user.setName(System.getProperty ("user.name")); user.setNick(System.getProperty ("user.name")); List<String> perms = Arrays.asList("read","write","grant"); perms.forEach(p -> {sess.save (new GLPermission (p)); user.grant (new GLPermission (p));}); sess.save (user); txn.commit(); } sess.close (); }
@Before public void setUp() throws Exception { db = new DB(); db.createSchema("schema.sql", true); db.open(); }
public int prepare (long id, Serializable o) { int rc = ABORTED; Context ctx = (Context) o; try { DB db = getDB (ctx); db.open (); ctx.put (TX, db.beginTransaction(timeout)); checkPoint (ctx); rc = PREPARED; } catch (Throwable t) { error (t); ctx.remove (DB); // "Close" participant checks // for DB in Context } return rc | NO_JOIN | READONLY; } public void commit (long id, Serializable o) { }
private void reset() { try (DB db = new DB()) { SeqNoManager mgr = new SeqNoManager(db); db.open(); db.beginTransaction(); mgr.reset("sync", 0L); db.commit(); } } }
private long next(long delay) { try (DB db = new DB()) { SeqNoManager mgr = new SeqNoManager(db); db.open(); db.beginTransaction(); long l = mgr.next("sync", 999999L); ISOUtil.sleep(delay); db.commit(); return l; } }