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;
}
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;
}
}
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;
}
}
}
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;
}
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);
}
}
}
}
Aggregations