/** * Opens a new connection based on JDBC properties and attaches it to a current thread. * * @param driver class name of driver * @param url URL connection to DB * @param user user name. * @param password password. */ public static DB open(String driver, String url, String user, String password) { return new DB(DB.DEFAULT_NAME).open(driver, url, user, password); }
/** * Opens local transaction. */ public static void openTransaction() { new DB(DB.DEFAULT_NAME).openTransaction(); }
/** * Use to check if there is a default connection present on current thread. * * @return true if finds default connection on current thread, false if not. */ public static boolean hasConnection() { return new DB(DB.DEFAULT_NAME).hasConnection(); }
@Before @BeforeEach public final void openTestConnections() { if(!suppressDb){ loadConfiguration("/database.properties"); List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); if (connectionWrappers.isEmpty()) { LOGGER.warn("no DB connections are configured, none opened"); return; } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if (rollback()){ db.openTransaction(); } } } }
/** * Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Supplier</code> * and then will close the connection. The connection to open is the same as in {@link #open(String)} method. * * <p></p> * * Example of usage: * <pre> Object result = withDb(jndiName, () -> { //place code here return res; }); * </pre> * * @param jndiName name of a JNDI connection from container * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(String jndiName, Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else { try (DB db = open(jndiName)){ return supplier.get(); } } }
@After @AfterEach public final void closeTestConnections() { if(!suppressDb){ List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { String dbName = connectionWrapper.getDbName(); DB db = new DB(dbName); if (rollback()) { db.rollbackTransaction(); } db.close(); } clearConnectionWrappers(); } } }
/** * Closes all current connections. */ public static void closeAllConnections(){ List<String> names = getCurrrentConnectionNames(); for(String name: names){ new DB(name).close(); } }
@Override public void onException(Exception e) { if(Configuration.isTesting()) return; List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(); if (connectionWrappers != null && !connectionWrappers.isEmpty()) { for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); if (db.hasConnection()) { if (manageTransaction) { db.rollbackTransaction(); logDebug("Rolling back transaction due to exception: " + e); } db.close(); } } } }
@Override public void after() { if(Configuration.isTesting()) return; List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(); if (connectionWrappers != null && !connectionWrappers.isEmpty()) { for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); if(db.hasConnection()){ if(manageTransaction){ if (RequestContext.exceptionHappened()) { logDebug("Skip commit transaction because already rolled back."); } else { db.commitTransaction(); } } db.close(); } } } }
/** * Rolls back local transaction. */ public static void rollbackTransaction() { new DB(DB.DEFAULT_NAME).rollbackTransaction(); }
/** * Opens a new connection in case additional driver-specific parameters need to be passed in. * * @param driver driver class name * @param url JDBC URL * @param props connection properties */ public DB open(String driver, String url, Properties props) { return open(driver, url, null, null, props); }
/** * Commits local transaction. */ public static void commitTransaction() { new DB(DB.DEFAULT_NAME).commitTransaction(); }
/** * Adds a batch statement using given <code>java.sql.PreparedStatement</code> and parameters. * @param ps <code>java.sql.PreparedStatement</code> to add batch to. * @param parameters parameters for the query in <code>java.sql.PreparedStatement</code>. Parameters will be * set on the statement in the same order as provided here. */ public static void addBatch(PreparedStatement ps, Object... parameters) { new DB(DB.DEFAULT_NAME).addBatch(ps, parameters); }
/** * This method is used internally by framework. * * @param spec specification for a JDBC connection. */ private DB openJndi(ConnectionJndiSpec spec) { if(spec.getContext() != null){ return openContext(spec.getContext(), spec.getDataSourceJndiName()); }else{ return open(spec.getDataSourceJndiName()); } }
/** * Runs a count query, returns a number of matching records. * * @param table table in which to count rows. * @param query this is a filtering query for the count. If '*' provided, all records will be counted. Example: * <code>"age > 65 AND department = 'accounting'"</code> * @param params parameters for placeholder substitution. * @return copunt number of records found in a table. */ public static Long count(String table, String query, Object... params) { return new DB(DB.DEFAULT_NAME).count(table, query, params); }
/** * Returns a value of the first column of the first row. * This query expects only one column selected in the select statement. * If more than one column returned, it will throw {@link IllegalArgumentException}. * * @param query query * @param params parameters * @return fetched value, or null if query did not fetch anything. */ public static Object firstCell(String query, Object... params) { return new DB(DB.DEFAULT_NAME).firstCell(query, params); }