public TransactionContext getTransactionContext() throws IOException { TransactionContext answer = new TransactionContext(this); if (transactionIsolation > 0) { answer.setTransactionIsolation(transactionIsolation); } return answer; }
@Override public void init() throws Exception { getAdapter().setUseExternalMessageReferences(isUseExternalMessageReferences()); if (isCreateTablesOnStartup()) { TransactionContext transactionContext = getTransactionContext(); transactionContext.getExclusiveConnection(); transactionContext.begin(); try { try { getAdapter().doCreateTables(transactionContext); } catch (SQLException e) { LOG.warn("Cannot create tables due to: " + e); JDBCPersistenceAdapter.log("Failure Details: ", e); } } finally { transactionContext.commit(); } } }
public void commit() throws IOException { if (!inTx) { throw new IOException("Not started."); } try { final boolean needsCommit = !connection.getAutoCommit(); executeBatch(); if (needsCommit) { connection.commit(); } } catch (SQLException e) { JDBCPersistenceAdapter.log("Commit failed: ", e); try { doRollback(); } catch (Exception ignored) {} IOException ioe = IOExceptionSupport.create(e); persistenceAdapter.getBrokerService().handleIOException(ioe); throw ioe; } finally { inTx = false; close(); } }
public void rollback() throws IOException { if (!inTx) { throw new IOException("Not started."); } try { doRollback(); } catch (SQLException e) { JDBCPersistenceAdapter.log("Rollback failed: ", e); throw IOExceptionSupport.create(e); } finally { inTx = false; close(); } }
@Override public void doAddMessageReference(TransactionContext c, long sequence, MessageId messageID, ActiveMQDestination destination, long expirationTime, String messageRef) throws SQLException, IOException { PreparedStatement s = c.getAddMessageStatement(); try { if (s == null) { s = c.getConnection().prepareStatement(this.statements.getAddMessageStatement()); if (this.batchStatements) { c.setAddMessageStatement(s); } } s.setLong(1, messageID.getBrokerSequenceId()); s.setString(2, messageID.getProducerId().toString()); s.setLong(3, messageID.getProducerSequenceId()); s.setString(4, destination.getQualifiedName()); s.setLong(5, expirationTime); s.setString(6, messageRef); if (this.batchStatements) { s.addBatch(); } else if (s.executeUpdate() != 1) { throw new SQLException("Failed add a message"); } } finally { if (!this.batchStatements) { s.close(); } } }
@Override public void doSetLastAck(TransactionContext c, ActiveMQDestination destination, XATransactionId xid, String clientId, String subscriptionName, long seq, long priority) throws SQLException, IOException { PreparedStatement s = c.getUpdateLastAckStatement(); try { if (s == null) { s = c.getConnection().prepareStatement(xid == null ? this.statements.getUpdateDurableLastAckStatement() : this.statements.getUpdateDurableLastAckInTxStatement()); if (this.batchStatements) { c.setUpdateLastAckStatement(s);
PreparedStatement s = c.getRemovedMessageStatement(); try { if (s == null) { s = c.getConnection().prepareStatement(xid == null ? this.statements.getRemoveMessageStatement() : this.statements.getUpdateXidFlagStatement()); if (this.batchStatements) { c.setRemovedMessageStatement(s);
@Override public SubscriptionInfo[] getAllSubscriptions() throws IOException { TransactionContext c = persistenceAdapter.getTransactionContext(); try { return adapter.doGetAllSubscriptions(c, destination); } catch (SQLException e) { JDBCPersistenceAdapter.log("JDBC Failure: ", e); throw IOExceptionSupport.create("Failed to lookup subscriptions. Reason: " + e, e); } finally { c.close(); } }
public void begin() throws IOException { if (inTx) { throw new IOException("Already started."); } inTx = true; connection = getConnection(); }
public void executeBatch() throws SQLException { try { executeBatch(addMessageStatement, "Failed add a message"); } finally { addMessageStatement = null; try { executeBatch(removedMessageStatement, "Failed to remove a message"); } finally { removedMessageStatement = null; try { executeBatch(updateLastAckStatement, "Failed to ack a message"); } finally { updateLastAckStatement = null; } } } }
public void commit() throws IOException { if (!inTx) { throw new IOException("Not started."); } try { executeBatch(); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { JDBCPersistenceAdapter.log("Commit failed: ", e); this.rollback(); throw IOExceptionSupport.create(e); } finally { inTx = false; close(); } }
@Override public void commitTransaction(ConnectionContext context) throws IOException { TransactionContext transactionContext = getTransactionContext(context); transactionContext.commit(); }
@Override public void rollbackTransaction(ConnectionContext context) throws IOException { TransactionContext transactionContext = getTransactionContext(context); transactionContext.rollback(); }
@Override public void beginTransaction(ConnectionContext context) throws IOException { TransactionContext transactionContext = getTransactionContext(context); transactionContext.begin(); }
public void close() throws IOException { if (!inTx) { try { // can be null for topic ops that bypass the store via existing cursor state if (connection != null) { final boolean needsCommit = !connection.getAutoCommit(); executeBatch(); if (needsCommit) { connection.commit(); } } } catch (SQLException e) { JDBCPersistenceAdapter.log("Error while closing connection: ", e); IOException ioe = IOExceptionSupport.create(e); persistenceAdapter.getBrokerService().handleIOException(ioe); throw ioe; } finally { silentClose(); for (Runnable completion: completions) { completion.run(); } completions.clear(); } } }
public void doAddMessage(TransactionContext c, long sequence, MessageId messageID, ActiveMQDestination destination, byte[] data, long expiration, byte priority, XATransactionId xid) throws SQLException, IOException { PreparedStatement s = c.getAddMessageStatement(); try { if (s == null) { s = c.getConnection().prepareStatement(this.statements.getAddMessageStatement()); if (this.batchStatements) { c.setAddMessageStatement(s);
@Override public void doSetLastAckWithPriority(TransactionContext c, ActiveMQDestination destination, XATransactionId xid, String clientId, String subscriptionName, long seq, long priority) throws SQLException, IOException { PreparedStatement s = c.getUpdateLastAckStatement(); try { if (s == null) { s = c.getConnection().prepareStatement(xid == null ? this.statements.getUpdateDurableLastAckWithPriorityStatement() : this.statements.getUpdateDurableLastAckWithPriorityInTxStatement()); if (this.batchStatements) { c.setUpdateLastAckStatement(s);
public void doRemoveMessage(TransactionContext c, long seq) throws SQLException, IOException { PreparedStatement s = c.getRemovedMessageStatement(); cleanupExclusiveLock.readLock().lock(); try { if (s == null) { s = c.getConnection().prepareStatement(this.statements.getRemoveMessageStatement()); if (this.batchStatments) { c.setRemovedMessageStatement(s); } } s.setLong(1, seq); if (this.batchStatments) { s.addBatch(); } else if (s.executeUpdate() != 1) { throw new SQLException("Failed to remove message"); } } finally { cleanupExclusiveLock.readLock().unlock(); if (!this.batchStatments && s != null) { s.close(); } } }
long[] getStoreSequenceIdForMessageId(ConnectionContext context, MessageId messageId, ActiveMQDestination destination) throws IOException { long[] result = new long[]{-1, Byte.MAX_VALUE -1}; TransactionContext c = getTransactionContext(context); try { result = adapter.getStoreSequenceId(c, destination, messageId); } catch (SQLException e) { JDBCPersistenceAdapter.log("JDBC Failure: ", e); throw IOExceptionSupport.create("Failed to get store sequenceId for messageId: " + messageId +", on: " + destination + ". Reason: " + e, e); } finally { c.close(); } return result; }