Class ExternalJdbcTransaction

java.lang.Object
io.ebeaninternal.server.transaction.ExternalJdbcTransaction
All Implemented Interfaces:
Transaction, SpiTransaction, TxnProfileEventCodes, AutoCloseable

public class ExternalJdbcTransaction extends Object
Transaction based on a java.sql.Connection supplied by an external transaction manager such as Spring.

This means that the transaction demarcation [commit(), rollback(), end()] must be controlled externally (by Spring etc) and so these methods [commit(), rollback(), end()] can not be called on this ExternalJdbcTransaction.

That is, a transaction started externally (by Spring etc) must be committed or rolled back externally as well.

  • Constructor Details

    • ExternalJdbcTransaction

      public ExternalJdbcTransaction(Connection connection)
      Create a Transaction that will have no transaction logging support.

      You need to create with a TransactionManager to have transaction logging.

    • ExternalJdbcTransaction

      public ExternalJdbcTransaction(boolean explicit, Connection connection, TransactionManager manager)
      Construct will all explicit parameters.
  • Method Details

    • commit

      public void commit()
      This will always throw a PersistenceException.

      Externally created connections should be committed or rolled back externally.

      Specified by:
      commit in interface Transaction
    • end

      public void end() throws jakarta.persistence.PersistenceException
      This will always throw a PersistenceException.

      Externally created connections should be committed or rolled back externally.

      Specified by:
      end in interface Transaction
      Throws:
      jakarta.persistence.PersistenceException
    • rollback

      public void rollback() throws jakarta.persistence.PersistenceException
      This will always throw a PersistenceException.

      Externally created connections should be committed or rolled back externally.

      Specified by:
      rollback in interface Transaction
      Throws:
      jakarta.persistence.PersistenceException
    • rollback

      public void rollback(Throwable e) throws jakarta.persistence.PersistenceException
      This will always throw a PersistenceException.

      Externally created connections should be committed or rolled back externally.

      Specified by:
      rollback in interface Transaction
      Throws:
      jakarta.persistence.PersistenceException
    • setLabel

      public final void setLabel(String label)
      Specified by:
      setLabel in interface Transaction
    • label

      public final String label()
      Description copied from interface: SpiTransaction
      Return the user defined label for the transaction.
      Specified by:
      label in interface SpiTransaction
    • startNanoTime

      public final long startNanoTime()
      Description copied from interface: SpiTransaction
      Return the start timestamp for the transaction (JVM side).
      Specified by:
      startNanoTime in interface SpiTransaction
    • profileOffset

      public final long profileOffset()
      Description copied from interface: SpiTransaction
      Return the offset time from the start of the transaction.
      Specified by:
      profileOffset in interface SpiTransaction
    • profileEvent

      public final void profileEvent(SpiProfileTransactionEvent event)
      Description copied from interface: SpiTransaction
      Check if the event should be added to a profiling transaction.
      Specified by:
      profileEvent in interface SpiTransaction
    • setProfileStream

      public final void setProfileStream(ProfileStream profileStream)
      Description copied from interface: SpiTransaction
      Set the profileStream to catch and time all the events for this transaction.
      Specified by:
      setProfileStream in interface SpiTransaction
    • profileStream

      public final ProfileStream profileStream()
      Description copied from interface: SpiTransaction
      Return the stream that profiling events are written to.
      Specified by:
      profileStream in interface SpiTransaction
    • setProfileLocation

      public final void setProfileLocation(ProfileLocation profileLocation)
      Description copied from interface: SpiTransaction
      Set the profile location for this transaction.
      Specified by:
      setProfileLocation in interface SpiTransaction
    • profileLocation

      public final ProfileLocation profileLocation()
      Description copied from interface: SpiTransaction
      Return the profile location for this transaction.
      Specified by:
      profileLocation in interface SpiTransaction
    • setAutoPersistUpdates

      public final void setAutoPersistUpdates(boolean autoPersistUpdates)
      Specified by:
      setAutoPersistUpdates in interface Transaction
    • isAutoPersistUpdates

      public final boolean isAutoPersistUpdates()
      Description copied from interface: SpiTransaction
      Return true if dirty beans are automatically persisted.
      Specified by:
      isAutoPersistUpdates in interface SpiTransaction
    • isSkipCacheExplicit

      public final boolean isSkipCacheExplicit()
      Description copied from interface: SpiTransaction
      Return true if explicitly set to skip cache (ignores skipOnWrite).
      Specified by:
      isSkipCacheExplicit in interface SpiTransaction
    • isSkipCache

      public final boolean isSkipCache()
      Specified by:
      isSkipCache in interface Transaction
    • setSkipCache

      public final void setSkipCache(boolean skipCache)
      Specified by:
      setSkipCache in interface Transaction
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • addBeanChange

      public final void addBeanChange(BeanChange beanChange)
      Description copied from interface: SpiTransaction
      Add a bean change to the change log.
      Specified by:
      addBeanChange in interface SpiTransaction
    • sendChangeLog

      public final void sendChangeLog(ChangeSet changesRequest)
      Description copied from interface: SpiTransaction
      Send the change set to be prepared and then logged.
      Specified by:
      sendChangeLog in interface SpiTransaction
    • register

      public final void register(TransactionCallback callback)
      Specified by:
      register in interface Transaction
    • registerDeferred

      public final void registerDeferred(PersistDeferredRelationship derived)
      Description copied from interface: SpiTransaction
      Register a "Deferred Relationship" that requires an additional update later.
      Specified by:
      registerDeferred in interface SpiTransaction
    • registerDeleteBean

      public final void registerDeleteBean(Integer persistingBean)
      Add a bean to the registed list.

      This is to handle bi-directional relationships where both sides Cascade.

      Specified by:
      registerDeleteBean in interface SpiTransaction
    • isRegisteredDeleteBean

      public final boolean isRegisteredDeleteBean(Integer persistingBean)
      Return true if this is a bean that has already been saved/deleted.
      Specified by:
      isRegisteredDeleteBean in interface SpiTransaction
    • unregisterBeans

      public final void unregisterBeans()
      Unregister the persisted beans (when persisting at the top level).
      Specified by:
      unregisterBeans in interface SpiTransaction
    • isRegisteredBean

      public final boolean isRegisteredBean(Object bean)
      Return true if this is a bean that has already been saved. This will register the bean if it is not already.
      Specified by:
      isRegisteredBean in interface SpiTransaction
    • isSaveAssocManyIntersection

      public final boolean isSaveAssocManyIntersection(String intersectionTable, String beanName)
      Return true if the m2m intersection save is allowed from a given bean direction. This is to stop m2m intersection management via both directions of a m2m.
      Specified by:
      isSaveAssocManyIntersection in interface SpiTransaction
    • depth

      public final void depth(int diff)
      Description copied from interface: SpiTransaction
      Modify the current 'depth' of the transaction.

      As we cascade save or delete we traverse the object graph tree. Going up to Assoc Ones the depth decreases and going down to Assoc Manys the depth increases.

      The depth is used for ordering batching statements. The lowest depth get executed first during save.

      Specified by:
      depth in interface SpiTransaction
    • depth

      public final int depth()
      Description copied from interface: SpiTransaction
      Return the current depth.
      Specified by:
      depth in interface SpiTransaction
    • depthDecrement

      public final void depthDecrement()
      Description copied from interface: SpiTransaction
      Decrement the depth BUT only if depth is greater than 0. Return the amount that depth should be incremented by (0 or 1).
      Specified by:
      depthDecrement in interface SpiTransaction
    • depthReset

      public final void depthReset()
      Description copied from interface: SpiTransaction
      Reset the depth back to 0 - done at the end of top level insert and update.
      Specified by:
      depthReset in interface SpiTransaction
    • markNotQueryOnly

      public final void markNotQueryOnly()
      Description copied from interface: SpiTransaction
      Mark the transaction explicitly as not being query only.
      Specified by:
      markNotQueryOnly in interface SpiTransaction
    • isReadOnly

      public boolean isReadOnly()
      Specified by:
      isReadOnly in interface Transaction
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Specified by:
      setReadOnly in interface Transaction
    • setUpdateAllLoadedProperties

      public final void setUpdateAllLoadedProperties(boolean updateAllLoadedProperties)
      Specified by:
      setUpdateAllLoadedProperties in interface Transaction
    • isUpdateAllLoadedProperties

      public final Boolean isUpdateAllLoadedProperties()
      Description copied from interface: SpiTransaction
      Return true if this transaction has updateAllLoadedProperties set. If null is returned the server default is used (set on DatabaseConfig).
      Specified by:
      isUpdateAllLoadedProperties in interface SpiTransaction
    • setBatchMode

      public final void setBatchMode(boolean batchMode)
      Specified by:
      setBatchMode in interface Transaction
    • isBatchMode

      public final boolean isBatchMode()
      Specified by:
      isBatchMode in interface Transaction
    • setBatchOnCascade

      public final void setBatchOnCascade(boolean batchMode)
      Specified by:
      setBatchOnCascade in interface Transaction
    • isBatchOnCascade

      public final boolean isBatchOnCascade()
      Specified by:
      isBatchOnCascade in interface Transaction
    • getBatchGetGeneratedKeys

      public final Boolean getBatchGetGeneratedKeys()
      Description copied from interface: SpiTransaction
      Return the getGeneratedKeys setting for this transaction.
      Specified by:
      getBatchGetGeneratedKeys in interface SpiTransaction
    • setGetGeneratedKeys

      public final void setGetGeneratedKeys(boolean getGeneratedKeys)
      Specified by:
      setGetGeneratedKeys in interface Transaction
    • setFlushOnMixed

      public final void setFlushOnMixed(boolean batchFlushOnMixed)
      Specified by:
      setFlushOnMixed in interface Transaction
    • getBatchSize

      public final int getBatchSize()
      Return the batchSize specifically set for this transaction or 0.

      Returning 0 implies to use the system wide default batch size.

      Specified by:
      getBatchSize in interface SpiTransaction
      Specified by:
      getBatchSize in interface Transaction
    • setBatchSize

      public final void setBatchSize(int batchSize)
      Specified by:
      setBatchSize in interface Transaction
    • isFlushOnQuery

      public final boolean isFlushOnQuery()
      Specified by:
      isFlushOnQuery in interface Transaction
    • setFlushOnQuery

      public final void setFlushOnQuery(boolean batchFlushOnQuery)
      Specified by:
      setFlushOnQuery in interface Transaction
    • isBatchThisRequest

      public final boolean isBatchThisRequest()
      Return true if this request should be batched. Returning false means that this request should be executed immediately.
      Specified by:
      isBatchThisRequest in interface SpiTransaction
    • checkBatchEscalationOnCollection

      public final void checkBatchEscalationOnCollection()
      Description copied from interface: SpiTransaction
      Potentially escalate batch mode on saving or deleting a collection.
      Specified by:
      checkBatchEscalationOnCollection in interface SpiTransaction
    • flushBatchOnCollection

      public final void flushBatchOnCollection()
      Description copied from interface: SpiTransaction
      Flush batch if we escalated batch mode on saving or deleting a collection.
      Specified by:
      flushBatchOnCollection in interface SpiTransaction
    • translate

      public final jakarta.persistence.PersistenceException translate(String message, SQLException cause)
      Description copied from interface: SpiTransaction
      Translate the SQLException.
      Specified by:
      translate in interface SpiTransaction
    • flushBatchOnCascade

      public final void flushBatchOnCascade()
      Flush after completing persist cascade.
      Specified by:
      flushBatchOnCascade in interface SpiTransaction
    • flushBatchOnRollback

      public final void flushBatchOnRollback()
      Description copied from interface: SpiTransaction
      If batch was on then effectively clear the batch such that we can handle exceptions and continue.
      Specified by:
      flushBatchOnRollback in interface SpiTransaction
    • checkBatchEscalationOnCascade

      public final boolean checkBatchEscalationOnCascade(PersistRequestBean<?> request)
      Description copied from interface: SpiTransaction
      Return true if batch mode got escalated for this request (and associated cascades).
      Specified by:
      checkBatchEscalationOnCascade in interface SpiTransaction
    • batchControl

      public final BatchControl batchControl()
      Description copied from interface: SpiTransaction
      Return the BatchControl used to batch up persist requests.
      Specified by:
      batchControl in interface SpiTransaction
    • setBatchControl

      public final void setBatchControl(BatchControl batchControl)
      Set the BatchControl to the transaction. This is done once per transaction on the first persist request.
      Specified by:
      setBatchControl in interface SpiTransaction
    • flush

      public final void flush()
      Flush any queued persist requests.

      This is general will result in a number of batched PreparedStatements executing.

      Specified by:
      flush in interface Transaction
    • persistenceContext

      public final SpiPersistenceContext persistenceContext()
      Return the persistence context associated with this transaction.
      Specified by:
      persistenceContext in interface SpiTransaction
    • setPersistenceContext

      public final void setPersistenceContext(SpiPersistenceContext context)
      Set the persistence context to this transaction.

      This could be considered similar to EJB3 Extended PersistanceContext. In that you get the PersistanceContext from a transaction, hold onto it, and then set it back later to a second transaction.

      Specified by:
      setPersistenceContext in interface SpiTransaction
    • event

      public final TransactionEvent event()
      Return the underlying TransactionEvent.
      Specified by:
      event in interface SpiTransaction
    • isExplicit

      public final boolean isExplicit()
      Return true if this was an explicitly created transaction.
      Specified by:
      isExplicit in interface SpiTransaction
    • isLogSql

      public final boolean isLogSql()
      Description copied from interface: SpiTransaction
      Return true if generated SQL and Bind values should be logged to the transaction log.
      Specified by:
      isLogSql in interface SpiTransaction
    • isLogSummary

      public final boolean isLogSummary()
      Description copied from interface: SpiTransaction
      Return true if summary level events should be logged to the transaction log.
      Specified by:
      isLogSummary in interface SpiTransaction
    • logSql

      public void logSql(String msg, Object... args)
      Description copied from interface: SpiTransaction
      Log a message to the SQL logger.
      Specified by:
      logSql in interface SpiTransaction
    • logSummary

      public final void logSummary(String msg, Object... args)
      Description copied from interface: SpiTransaction
      Log a summary message to the SUMMARY logger.
      Specified by:
      logSummary in interface SpiTransaction
    • logTxn

      public void logTxn(String msg, Object... args)
      Description copied from interface: SpiTransaction
      Log a transaction message to the transaction logger.
      Specified by:
      logTxn in interface SpiTransaction
    • id

      public final String id()
      Return the transaction id.
      Specified by:
      id in interface SpiTransaction
    • setTenantId

      public final void setTenantId(Object tenantId)
      Description copied from interface: SpiTransaction
      Set the current Tenant Id.
      Specified by:
      setTenantId in interface SpiTransaction
    • tenantId

      public final Object tenantId()
      Description copied from interface: SpiTransaction
      Return the current Tenant Id.
      Specified by:
      tenantId in interface SpiTransaction
    • internalConnection

      public Connection internalConnection()
      Return the underlying connection for internal use.
      Specified by:
      internalConnection in interface SpiTransaction
    • connection

      public Connection connection()
      Return the underlying connection for public use.
      Specified by:
      connection in interface Transaction
    • postCommit

      public final void postCommit()
      Description copied from interface: SpiTransaction
      Fire post commit events and listeners.
      Specified by:
      postCommit in interface SpiTransaction
    • preCommit

      public final void preCommit()
      Description copied from interface: SpiTransaction
      Fire pre commit processing/listeners.
      Specified by:
      preCommit in interface SpiTransaction
    • commitAndContinue

      public void commitAndContinue()
      Perform a commit, fire callbacks and notify l2 cache etc.

      This leaves the transaction active and expects another commit to occur later (which closes the underlying connection etc).

      Specified by:
      commitAndContinue in interface Transaction
    • isRollbackOnly

      public final boolean isRollbackOnly()
      Return true if the transaction is marked as rollback only.
      Specified by:
      isRollbackOnly in interface Transaction
    • setRollbackOnly

      public final void setRollbackOnly()
      Mark the transaction as rollback only.
      Specified by:
      setRollbackOnly in interface Transaction
    • isNestedUseSavepoint

      public final boolean isNestedUseSavepoint()
      Description copied from interface: SpiTransaction
      Return true when nested transactions should create Savepoints.
      Specified by:
      isNestedUseSavepoint in interface SpiTransaction
    • setNestedUseSavepoint

      public final void setNestedUseSavepoint()
      Specified by:
      setNestedUseSavepoint in interface Transaction
    • rollbackAndContinue

      public void rollbackAndContinue()
      Specified by:
      rollbackAndContinue in interface Transaction
    • postRollback

      public final void postRollback(Throwable cause)
      Description copied from interface: SpiTransaction
      Fire post rollback events and listeners.
      Specified by:
      postRollback in interface SpiTransaction
    • isActive

      public boolean isActive()
      Return true if the transaction is active.
      Specified by:
      isActive in interface Transaction
    • deactivateExternal

      public void deactivateExternal()
      Description copied from interface: SpiTransaction
      Set the transaction to be inactive via external transaction manager.
      Specified by:
      deactivateExternal in interface SpiTransaction
    • isPersistCascade

      public final boolean isPersistCascade()
      Description copied from interface: SpiTransaction
      Whether persistCascade is on for save and delete.
      Specified by:
      isPersistCascade in interface SpiTransaction
    • setPersistCascade

      public final void setPersistCascade(boolean persistCascade)
      Specified by:
      setPersistCascade in interface Transaction
    • addModification

      public final void addModification(String tableName, boolean inserts, boolean updates, boolean deletes)
      Specified by:
      addModification in interface Transaction
    • putUserObject

      public final void putUserObject(String name, Object value)
      Specified by:
      putUserObject in interface Transaction
    • getUserObject

      public final Object getUserObject(String name)
      Specified by:
      getUserObject in interface Transaction
    • close

      public final void close()
      Alias for end(), which enables this class to be used in try-with-resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Transaction