/** {@inheritDoc} */ @Override public void setUsed(boolean used) { log().t(poolId,id,"used",used); super.setUsed(used); synchronized (this) { this.used = used; if (!used) // for security reasons - remove old garbage in the session try { if (connection != null) connection.rollback(); } catch (Throwable e) { log().d(e); close(); } } }
JdbcStatement(JdbcConnection dbCon, String query,String language) throws MException { this.original = query; this.dbCon = dbCon; this.query = dbCon.createQueryCompiler(language).compileString(query); }
private void validateSth() throws Exception { synchronized (this) { if (sth==null || sth.isClosed()) { Connection con = dbCon.getConnection(); sth = con.createStatement(); } } }
@Override public InternalDbConnection createConnection() throws Exception { // ResourceNode concon = config.getNode("connection"); String driver = config.getExtracted("driver"); String url = config.getExtracted("url"); String user = config.getExtracted("user"); String pass = config.getExtracted("pass"); if (!MString.isEmpty(driver)) { if (activator != null) activator.getClazz(driver); else Class.forName(driver); } log().t(driver,url,user); Connection con = DriverManager.getConnection(url,user,MPassword.decode(pass)); getDialect().prepareConnection(con); JdbcConnection dbCon = new JdbcConnection(this,con); long timeoutUnused = MPeriod.toMilliseconds( config.getExtracted("timeout_unused"), 0 ); long timeoutLifetime = MPeriod.toMilliseconds( config.getExtracted("timeout_lifetime"), 0 ); if (timeoutUnused > 0) dbCon.setTimeoutUnused(timeoutUnused); if (timeoutLifetime > 0) dbCon.setTimeoutLifetime(timeoutLifetime); return dbCon; }
/** {@inheritDoc} */ @Override public void rollback() throws Exception { log().t(poolId,id,"rollback"); if (closed) throw new IOException("Connection not valid"); connection.rollback(); }
/** {@inheritDoc} */ @Override protected void finalize() throws Throwable { close(); super.finalize(); }
/** {@inheritDoc} */ @Override public InternalDbConnection createConnection() throws Exception { if (dataSource == null) return null; Connection con = null; try { con = dataSource.getConnection(); } catch (Exception e) { // special behavior for e.g. mysql, retry to get a connection after gc() // Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections if (e.getMessage() != null && e.getMessage().indexOf("Too many connections") > -1) { System.gc(); con = dataSource.getConnection(); } else throw e; } getDialect().prepareConnection(con); return new JdbcConnection(this, con); }
/** {@inheritDoc} */ @Override public DbStatement createStatement(String sql) throws MException { return createStatement(sql, provider.getDialect().detectLanguage(sql)); }
protected PreparedStatement prepareStatement(Map<String, Object> attributes, Statement sth, String query ) throws SQLException { // recycle prepared query - should not differ ! if (xquery != null & preparedSth != null && xquery.equals(query)) return preparedSth; // if differ close last prepared query closePreparedSth(); // checkout new if (attributes != null && attributes.containsKey( RETURN_BINARY_KEY + "0")) { PreparedStatement psth = dbCon.getConnection().prepareStatement(query); xquery = query; for (int nr = 0; attributes.containsKey(RETURN_BINARY_KEY + nr); nr++) { psth.setBinaryStream(nr+1, (InputStream) attributes.get(RETURN_BINARY_KEY + nr)); attributes.remove(RETURN_BINARY_KEY + nr); } return psth; } return null; }
/** {@inheritDoc} */ @Override public void commit() throws Exception { log().t(poolId,id,"commit"); if (closed) throw new MException(poolId,id,"Connection not valid"); if (!connection.getAutoCommit()) connection.commit(); }
Connection con = ((JdbcConnection)db.instance()).getConnection(); Statement sth = con.createStatement();
public void createIndexes(IConfig data, DbConnection db, MetadataBundle caoMeta, boolean cleanup) throws Exception { Connection con = ((JdbcConnection)db.instance()).getConnection(); Statement sth = con.createStatement(); DatabaseMetaData meta = con.getMetaData();
public void createTables(IConfig data, DbConnection db, MetadataBundle caoBundle, boolean cleanup) throws Exception { Connection con = ((JdbcConnection)db.instance()).getConnection(); Statement sth = con.createStatement(); DatabaseMetaData meta = con.getMetaData();