/** * Returns the default datasource. */ public static DataSource getDataSource() { return getDataSource("default"); }
/** * Returns a connection from the default datasource, * with auto-commit enabled. */ public static Connection getConnection() { return getConnection("default"); }
/** * Close all the open connections for the current thread. */ public static void close() { close(DEFAULT); }
private static void disableForeignKeyConstraints() { if (DBPlugin.url.startsWith("jdbc:oracle:")) { DB.execute("begin\n" + "for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'\n" + "and status = 'ENABLED') LOOP\n" + "execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';\n" + "end loop;\n" DB.execute("SET REFERENTIAL_INTEGRITY FALSE"); return; DB.execute("SET REFERENTIAL_INTEGRITY FALSE"); return; DB.execute("SET foreign_key_checks = 0;"); return; DB.execute("SET CONSTRAINTS ALL DEFERRED"); return; try { List<String> names = new ArrayList<>(); Connection connection = DB.getConnection();
/** * Open a connection for the current thread. * * @param name * Name of the DB * @return A valid SQL connection */ public static Connection getConnection(String name) { try { if (JPA.isEnabled()) { return ((SessionImpl) ((HibernateEntityManager) JPA.em(name)).getSession()).connection(); } Connection localConnection = getLocalConnection(name); if (localConnection != null) { return localConnection; } // We have no connection Connection connection = getDataSource(name).getConnection(); registerLocalConnection(name, connection); return connection; } catch (NullPointerException e) { if (getDataSource(name) == null) { throw new DatabaseException("No database found. Check the configuration of your application.", e); } throw e; } catch (Exception e) { // Exception throw new DatabaseException(e.getMessage()); } }
/** * Execute an SQL query * * @param name * the DB name * @param SQL * the SQL statement * @return The rowSet of the query */ public static RowSet executeQuery(String name, String SQL) { Statement statement = null; ResultSet rs = null; try { statement = getConnection(name).createStatement(); if (statement != null) { rs = statement.executeQuery(SQL); } // Need to use a CachedRowSet that caches its rows in memory, which // makes it possible to operate without always being connected to // its data source CachedRowSet rowset = new CachedRowSetImpl(); rowset.populate(rs); return rowset; } catch (SQLException ex) { throw new DatabaseException(ex.getMessage(), ex); } finally { safeCloseResultSet(rs); safeCloseStatement(statement); } }
/** * Execute an SQL update * * @param name * the DB name * @param SQL * the SQL statement * @return true if the next result is a ResultSet object; false if it is an update count or there are no more * results */ public static boolean execute(String name, String SQL) { Statement statement = null; try { statement = getConnection(name).createStatement(); if (statement != null) { return statement.execute(SQL); } } catch (SQLException ex) { throw new DatabaseException(ex.getMessage(), ex); } finally { safeCloseStatement(statement); } return false; }
/** * Executes a block of code, providing a JDBC connection. * The connection and all created statements are automatically released. * * @param name Datasource name * @param autocommit Auto-commit setting * @param block Code block to execute * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>) */ public static <A> A withConnection(String name, boolean autocommit, ConnectionCallable<A> block, Application application) { return play.api.db.DB.withConnection(name, autocommit, connectionFunction(block), application); }
@Override public void onApplicationStop() { if (Play.mode.isProd()) { DB.destroyAll(); } }
/** * Destroy the datasource */ public static void destroy() { destroy(DEFAULT); }
@Override public void invocationFinally() { DB.closeAll(); }
private static void enableForeignKeyConstraints() { if (DBPlugin.url.startsWith("jdbc:oracle:")) { DB.execute("begin\n" + "for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'\n" + "and status = 'DISABLED') LOOP\n" + "execute immediate 'alter table '||i.table_name||' enable constraint '||i.constraint_name||'';\n" + "end loop;\n" DB.execute("SET REFERENTIAL_INTEGRITY TRUE"); return; DB.execute("SET REFERENTIAL_INTEGRITY TRUE"); return; DB.execute("SET foreign_key_checks = 1;"); return; Statement exec = null; try { connect = DB.getConnection();
/** * Execute a block of code, in the scope of a JDBC transaction. * The connection and all created statements are automatically released. * The transaction is automatically committed, unless an exception occurs. * * @param name Datasource name * @param block Code block to execute * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>) */ public static void withTransaction(String name, ConnectionRunnable block, Application application) { play.api.db.DB.withTransaction(name, connectionFunction(block), application); }
/** * Destroy all datasources */ public static void destroyAll() { Set<String> keySet = new HashSet<>(datasources.keySet()); for (String name : keySet) { destroy(name); } } }
/** * Returns the default datasource. */ public static DataSource getDataSource() { return getDataSource("default"); }
/** * Returns a connection from the default datasource, * with the specified auto-commit setting. */ public static Connection getConnection(boolean autocommit) { return getConnection("default", autocommit); }
/** * Flush the entire JDBC database */ public static void deleteDatabase() { try { idCache.clear(); List<String> names = new ArrayList<>(); ResultSet rs = DB.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { String name = rs.getString("TABLE_NAME"); names.add(name); } disableForeignKeyConstraints(); for (String name : names) { if (Arrays.binarySearch(dontDeleteTheseTables, name) < 0) { if (Logger.isTraceEnabled()) { Logger.trace("Dropping content of table %s", name); } DB.execute(getDeleteTableStmt(name) + ";"); } } enableForeignKeyConstraints(); Play.pluginCollection.afterFixtureLoad(); } catch (Exception e) { throw new RuntimeException("Cannot delete all table data : " + e.getMessage(), e); } }
/** * Execute a block of code, in the scope of a JDBC transaction. * The connection and all created statements are automatically released. * The transaction is automatically committed, unless an exception occurs. * * @param name Datasource name * @param block Code block to execute * @param application Play application (<tt>play.api.Play.unsafeApplication()</tt>) */ public static <A> A withTransaction(String name, ConnectionCallable<A> block, Application application) { return play.api.db.DB.withTransaction(name, connectionFunction(block), application); }
/** * Close all the open connections for the current thread. */ public static void closeAll() { Map<String, Connection> map = localConnection.get(); if (map != null) { Set<String> keySet = new HashSet<>(map.keySet()); for (String name : keySet) { close(name); } } }