/** * <p>Overrides finalize(). If called, it closes the connection.</p> * * <p>This was done at the request of <a href="mailto:rachel@enlarion.demon.co.uk">Rachel * Greenham</a> who hit a problem where multiple clients didn't close the connection, and once a * fortnight enough clients were open to kill the postgres server.</p> */ protected void finalize() throws Throwable { try { if (openStackTrace != null) { LOGGER.log(Level.WARNING, GT.tr("Finalizing a Connection that was never closed:"), openStackTrace); } close(); } finally { super.finalize(); } }
@Override public int getDatabaseMajorVersion() throws SQLException { return connection.getServerMajorVersion(); }
@Override public void addDataType(String type, String name) { try { addDataType(type, Class.forName(name).asSubclass(PGobject.class)); } catch (Exception e) { throw new RuntimeException("Cannot register new type: " + e); } }
@Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { checkClosed(); return createStatement(resultSetType, resultSetConcurrency, getHoldability()); }
public String getSchema() throws SQLException { checkClosed(); Statement stmt = createStatement(); try { ResultSet rs = stmt.executeQuery("select current_schema()"); try { if (!rs.next()) { return null; // Is it ever possible? } return rs.getString(1); } finally { rs.close(); } } finally { stmt.close(); } }
public void setSchema(String schema) throws SQLException { checkClosed(); Statement stmt = createStatement(); try { if (schema == null) { stmt.executeUpdate("SET SESSION search_path TO DEFAULT"); } else { StringBuilder sb = new StringBuilder(); sb.append("SET SESSION search_path TO '"); Utils.escapeLiteral(sb, schema, getStandardConformingStrings()); sb.append("'"); stmt.executeUpdate(sb.toString()); LOGGER.log(Level.FINE, " setSchema = {0}", schema); } } finally { stmt.close(); } }
@Test @FixFor("DBZ-934") public void temporaryReplicationSlotsShouldGetDroppedAutomatically() throws Exception { try(ReplicationConnection replicationConnection = TestHelper.createForReplication("test", true)) { PgConnection pgConnection = getUnderlyingConnection(replicationConnection); // temporary replication slots are not supported by Postgres < 10 if (pgConnection.getServerMajorVersion() < 10) { return; } // simulate ungraceful shutdown by closing underlying database connection pgConnection.close(); try (PostgresConnection connection = TestHelper.create()) { assertFalse("postgres did not drop replication slot", connection.dropReplicationSlot("test")); } } }
+ " ORDER BY a.attnum "; Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int typeOid = (int) rs.getLong("atttypid"); int typeMod = rs.getInt("atttypmod"); int decimalDigits = connection.getTypeInfo().getScale(typeOid, typeMod); int columnSize = connection.getTypeInfo().getPrecision(typeOid, typeMod); if (columnSize == 0) { columnSize = connection.getTypeInfo().getDisplaySize(typeOid, typeMod); tuple[0] = connection.encodeString(Integer.toString(scope)); tuple[1] = rs.getBytes("attname"); tuple[2] = connection.encodeString(Integer.toString(connection.getTypeInfo().getSQLType(typeOid))); tuple[3] = connection.encodeString(connection.getTypeInfo().getPGType(typeOid)); tuple[4] = connection.encodeString(Integer.toString(columnSize)); tuple[5] = null; // unused tuple[6] = connection.encodeString(Integer.toString(decimalDigits)); tuple[7] = connection.encodeString(Integer.toString(java.sql.DatabaseMetaData.bestRowNotPseudo)); v.add(tuple);
@Override public Savepoint setSavepoint(String name) throws SQLException { checkClosed(); if (getAutoCommit()) { throw new PSQLException(GT.tr("Cannot establish a savepoint in auto-commit mode."), PSQLState.NO_ACTIVE_SQL_TRANSACTION); } PSQLSavepoint savepoint = new PSQLSavepoint(name); // Note we can't use execSQLUpdate because we don't want // to suppress BEGIN. Statement stmt = createStatement(); stmt.executeUpdate("SAVEPOINT " + savepoint.getPGName()); stmt.close(); return savepoint; }
@Override public void setClientInfo(String name, String value) throws SQLClientInfoException { try { checkClosed(); } catch (final SQLException cause) { Map<String, ClientInfoStatus> failures = new HashMap<String, ClientInfoStatus>(); if (haveMinimumServerVersion(ServerVersion.v9_0) && "ApplicationName".equals(name)) { if (value == null) { value = ""; Utils.escapeLiteral(sql, value, getStandardConformingStrings()); sql.append("'"); execSQLUpdate(sql.toString()); } catch (SQLException sqle) { Map<String, ClientInfoStatus> failures = new HashMap<String, ClientInfoStatus>(); addWarning(new SQLWarning(GT.tr("ClientInfo property not supported."), PSQLState.NOT_IMPLEMENTED.getState()));
if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) { sql = "SELECT * FROM ("; } else { if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) { sql += "row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum, "; } else { if (connection.haveMinimumServerVersion(ServerVersion.v10)) { sql += "nullif(a.attidentity, '') as attidentity,"; } else { if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) { sql += ") c WHERE true "; Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { sqlType = Types.VARCHAR; } else { sqlType = connection.getTypeInfo().getSQLType(typeOid); tuple[4] = connection.encodeString(Integer.toString(sqlType)); String pgType = connection.getTypeInfo().getPGType(typeOid); tuple[5] = connection.encodeString(pgType); // Type name tuple[7] = null; // Buffer length if (pgType.equals("int4")) {
PSQLState.INVALID_PARAMETER_VALUE); if (isClosed()) { return false; Statement statement = createStatement(); statement.execute("IDENTIFY_SYSTEM"); statement.close(); } else { if (checkConnectionQuery == null) { checkConnectionQuery = prepareStatement("");
private CopyManager getCopyManager(Connection conn) throws SQLException { PgConnection unwrapped = conn.unwrap(PgConnection.class); return unwrapped.getCopyAPI(); }
connection.checkClosed(); if (keywords == null) { if (connection.haveMinimumServerVersion(ServerVersion.v9_0)) { ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery(sql); if (!rs.next()) {
+ (connection.haveMinimumServerVersion(ServerVersion.v8_4) ? "a.attacl, " : "") + " a.attname " + " FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c, " Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) { String acl = rs.getString("attacl"); Map<String, Map<String, List<String[]>>> relPermissions = parseACL(acl, owner); Arrays.sort(permNames); for (String permName : permNames) { byte[] privilege = connection.encodeString(permName); Map<String, List<String[]>> grantees = permissions.get(permName); for (Map.Entry<String, List<String[]>> userToGrantable : grantees.entrySet()) { tuple[2] = tableName; tuple[3] = column; tuple[4] = connection.encodeString(grants[0]); tuple[5] = connection.encodeString(grantee); tuple[6] = privilege; tuple[7] = connection.encodeString(grantable); v.add(tuple);
@Override public void execSQLUpdate(String s) throws SQLException { BaseStatement stmt = (BaseStatement) createStatement(); if (stmt.executeWithFlags(s, QueryExecutor.QUERY_NO_METADATA | QueryExecutor.QUERY_NO_RESULTS | QueryExecutor.QUERY_SUPPRESS_BEGIN)) { throw new PSQLException(GT.tr("A result was returned when none was expected."), PSQLState.TOO_MANY_RESULTS); } // Transfer warnings to the connection, since the user never // has a chance to see the statement itself. SQLWarning warnings = stmt.getWarnings(); if (warnings != null) { addWarning(warnings); } stmt.close(); }
try (Statement stmt = pgConnection().createStatement()) { stmt.execute(String.format( "CREATE_REPLICATION_SLOT %s TEMPORARY LOGICAL %s", pgConnection().getReplicationAPI() .createReplicationSlot() .logical()
@Override public Statement createStatement() throws SQLException { // We now follow the spec and default to TYPE_FORWARD_ONLY. return createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); }
private Charset determineDatabaseCharset() { try { return Charset.forName(((PgConnection) connection()).getEncoding().name()); } catch (SQLException e) { throw new RuntimeException("Couldn't obtain encoding for database " + database(), e); } }
Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { Arrays.sort(permNames); for (String permName : permNames) { byte[] privilege = connection.encodeString(permName); Map<String, List<String[]>> grantees = permissions.get(permName); for (Map.Entry<String, List<String[]>> userToGrantable : grantees.entrySet()) { tuple[1] = schema; tuple[2] = table; tuple[3] = connection.encodeString(grantor); tuple[4] = connection.encodeString(granteeUser); tuple[5] = privilege; tuple[6] = connection.encodeString(grantable); v.add(tuple);