/** * 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 DB open(String driver, String url, String user, String password) { return open(driver, url, user, password, null); }
/** * 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()); } }
@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(); } } } }
/** * 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); }
/** * 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 a connection from JNDI based on a registered name. This assumes that there is a <code>jndi.properties</code> * file with proper JNDI configuration in it. * * @param jndiName name of a configured data source. */ public static DB open(String jndiName) { return new DB(DB.DEFAULT_NAME).open(jndiName); }
/** * Opens a connection from a datasource. This methods gives a high level control while sourcing a DB connection. * * @param dataSource datasource will be used to acquire a connection. */ public static DB open(DataSource dataSource) { return new DB(DB.DEFAULT_NAME).open(dataSource); }
/** * This method will open a connection defined in the file 'database.properties' located at * root of classpath. The connection picked from the file is defined by <code>ACTIVE_ENV</code> * environment variable. If this variable is not defined, it defaults to 'development' environment. * * If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'. * */ public static DB open(){ return new DB(DB.DEFAULT_NAME).open(); }
/** * 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 static DB open(String driver, String url, Properties props) { return new DB(DB.DEFAULT_NAME).open(driver, url, props); }
/** * Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases * when file <code>jndi.properties</code> cannot be easily modified. * * @param jndiName name of JNDI data source. * @param jndiProperties JNDI properties */ public static DB open(String jndiName, Properties jndiProperties) { return new DB(DB.DEFAULT_NAME).open(jndiName, jndiProperties); }
private DB openDataSource(ConnectionDataSourceSpec spec) { return open(spec.getDataSource()); }
/** * 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(); } } }
/** * Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Supplier.get()</code> * and then will close the connection. The connection to open is the same as in {@link #open()} method. * * <p></p> * * Example of usage: * <pre> Object result = withDb(() -> { //place code here return res; // whatever it is }); * </pre> * * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else { try (DB db = open()){ return supplier.get(); } } } }
/** * Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code> * and then will close the connection. The connection to open is the same as in {@link #open(String, Properties)} method. * * <p></p> * * Example of usage: * <pre> Object result = withDb("jndiName1", props, () -> { //place code here return res; }); * </pre> * * @param jndiName name of a configured data source. * @param jndiProperties JNDI properties. * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(String jndiName, Properties jndiProperties, Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else{ try (DB db = open(jndiName, jndiProperties)){ return supplier.get(); } } }
/** * Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Supplier.get()</code> * and then will close the connection. The connection to open is the same as in * {@link #open(String, String, String, String)} method. * * <p></p> * * Example of usage: * <pre> Object result = withDb(driver, url, user, password, () -> { //place code here return val; }); * </pre> * * The arguments to this method are the same as to {@link #open(String, String, String, String)} method. * * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(String driver, String url, String user, String password, Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else{ try (DB db = open(driver, url, user, password)){ return supplier.get(); } } }
/** * 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, String, Properties)} method. * * <p></p> * * Example of usage: * <pre> Object results = withDb(driver, url, properties, () -> { //place code here return res; }); * </pre> * * The arguments to this method are the same as to {@link #open(String, String, Properties)} method. * * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(String driver, String url, Properties properties, Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else{ try (DB db = open(driver, url, properties)){ return supplier.get(); } } }
/** * Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code> * and then will close the connection. The connection to open is the same as in {@link #open(DataSource)} method. * * <p></p> * * Example of usage: * <pre> Object result = withDb(datasource, () -> { //place code here return res; }); * </pre> * * @param dataSource instance of <code>DataSource</code> to get a connection from. * @param supplier instance of <code>Supplier</code> to execute. */ public <T> T withDb(DataSource dataSource, Supplier<T> supplier) { if(hasConnection()){ return supplier.get(); }else{ try (DB db = open(dataSource)){ return supplier.get(); } } }
/** * This method is used internally by framework. * * @param spec specification for a JDBC connection. */ private DB openJdbc(ConnectionJdbcSpec spec) { if(spec.getProps()!= null){ return open(spec.getDriver(), spec.getUrl(), spec.getProps()); }else{ return open(spec.getDriver(), spec.getUrl(), spec.getUser(), spec.getPassword()); } }
/** * This method will open a connection defined in the file 'database.properties' located at * root of classpath. The connection picked up from the file is defined by <code>ACTIVE_ENV</code> * environment variable or <code>active_env</code> system property. * If this variable is not defined, it defaults to 'development' environment. * * <p></p> * If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'. * * @see Configuration#getEnvironment() */ public DB open(){ Configuration config = Registry.instance().getConfiguration(); ConnectionSpec spec = config.getCurrentConnectionSpec(); if(spec == null){ throw new DBException("Could not find configuration in a property file for environment: " + config.getEnvironment() + ". Are you sure you have a database.properties file configured?"); } return open(spec); }
/** * 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); }