Search in sources :

Example 6 with PGConnection

use of org.postgresql.PGConnection in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method getPostGresLargeObjectSize.

private int getPostGresLargeObjectSize(JDBCSequentialFile file) throws SQLException {
    LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
    int size = 0;
    Long oid = getOID(file);
    if (oid != null) {
        synchronized (connection) {
            try {
                connection.setAutoCommit(false);
                LargeObject largeObject = lobjManager.open(oid, LargeObjectManager.READ);
                size = largeObject.size();
                largeObject.close();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                throw e;
            }
        }
    }
    return size;
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) LargeObject(org.postgresql.largeobject.LargeObject)

Example 7 with PGConnection

use of org.postgresql.PGConnection in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method writeToFile.

@Override
public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException {
    synchronized (connection) {
        LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
        LargeObject largeObject = null;
        Long oid = getOID(file);
        try {
            connection.setAutoCommit(false);
            largeObject = lobjManager.open(oid, LargeObjectManager.WRITE);
            largeObject.seek(largeObject.size());
            largeObject.write(data);
            largeObject.close();
            connection.commit();
        } catch (Exception e) {
            connection.rollback();
            throw e;
        }
        return data.length;
    }
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) LargeObject(org.postgresql.largeobject.LargeObject) SQLException(java.sql.SQLException)

Example 8 with PGConnection

use of org.postgresql.PGConnection in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method readFromFile.

@Override
public int readFromFile(JDBCSequentialFile file, ByteBuffer bytes) throws SQLException {
    LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
    LargeObject largeObject = null;
    long oid = getOID(file);
    synchronized (connection) {
        try {
            connection.setAutoCommit(false);
            largeObject = lobjManager.open(oid, LargeObjectManager.READ);
            int readLength = (int) calculateReadLength(largeObject.size(), bytes.remaining(), file.position());
            if (readLength > 0) {
                if (file.position() > 0)
                    largeObject.seek((int) file.position());
                byte[] data = largeObject.read(readLength);
                bytes.put(data);
            }
            largeObject.close();
            connection.commit();
            return readLength;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    }
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) LargeObject(org.postgresql.largeobject.LargeObject)

Example 9 with PGConnection

use of org.postgresql.PGConnection in project solr-document-store by DBCDK.

the class QueueRulesDaemon method untanglePostgresConnection.

/**
 * UGLY!!!
 * <p>
 * Since com.sun.gjc.spi.jdbc40.ConnectionWrapper40 from fish.payara,
 * somehow crashed the compile phase of maven The reflection hack is needed.
 *
 * @param connection connection from datasource
 * @return postgres connection
 */
private PGConnection untanglePostgresConnection(Connection connection) {
    while (!(connection instanceof PGConnection)) {
        Class<? extends Connection> clazz = connection.getClass();
        try {
            Method method1 = clazz.getMethod("getConnection");
            if (method1 != null) {
                Object c1 = method1.invoke(connection);
                if (c1 == null) {
                    throw new RuntimeException("Cannot unwrap to postgresql connection");
                }
                connection = (Connection) c1;
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            log.error("Exception: {}", ex.getMessage());
            log.debug("Exception: ", ex);
        }
    }
    return (PGConnection) connection;
}
Also used : PGConnection(org.postgresql.PGConnection) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with PGConnection

use of org.postgresql.PGConnection in project solr-document-store by DBCDK.

the class QueueRulesDaemon method monitorLoop.

/**
 * Daemon loop
 */
private void monitorLoop() {
    int throttle_pos = 0;
    while (alive()) {
        try (Connection connection = dataSource.getConnection();
            Statement stmt = connection.createStatement()) {
            PGConnection pgConnection = untanglePostgresConnection(connection);
            stmt.executeUpdate("LISTEN queueNotify");
            readQueueRules();
            while (alive()) {
                if (pollNotification(pgConnection)) {
                    readQueueRules();
                }
                throttle_pos = 0;
            }
            stmt.executeUpdate("UNLISTEN queueNotify");
        } catch (SQLException ex) {
            log.error("Error communicating with database: {}", ex.getMessage());
            log.debug("Error communicating with database: ", ex);
        } catch (RuntimeException ex) {
            log.error("Runtime error: {}", ex.getMessage());
            log.debug("Runtime error: ", ex);
        }
        if (alive()) {
            try {
                long throttle = THROTTLE_SLEEP[throttle_pos++];
                if (throttle_pos >= THROTTLE_SLEEP.length) {
                    throttle_pos = THROTTLE_SLEEP.length - 1;
                }
                Thread.sleep(throttle * 1_000L);
            } catch (InterruptedException ex) {
                log.error("Exception: {}", ex.getMessage());
                log.debug("Exception: ", ex);
            }
        }
    }
}
Also used : PGConnection(org.postgresql.PGConnection) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection)

Aggregations

PGConnection (org.postgresql.PGConnection)16 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)7 LargeObjectManager (org.postgresql.largeobject.LargeObjectManager)5 IOException (java.io.IOException)4 LargeObject (org.postgresql.largeobject.LargeObject)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)2 DelegatingConnection (org.apache.commons.dbcp2.DelegatingConnection)2 PGCopyOutputStream (org.postgresql.copy.PGCopyOutputStream)2 Resource (com.revolsys.spring.resource.Resource)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Point (com.vividsolutions.jts.geom.Point)1 AbstractDBTest (eu.esdihumboldt.hale.io.jdbc.test.AbstractDBTest)1 JSonMapper (io.eventuate.javaclient.commonimpl.JSonMapper)1 BinLogEvent (io.eventuate.local.common.BinLogEvent)1