Class JdbcDatabaseManager

All Implemented Interfaces:
Flushable, AutoCloseable

public final class JdbcDatabaseManager extends AbstractDatabaseManager
An AbstractDatabaseManager implementation for relational databases accessed via JDBC.
  • Field Details

  • Constructor Details

  • Method Details

    • appendColumnName

      private static void appendColumnName(int i, String columnName, StringBuilder sb)
    • appendColumnNames

      private static void appendColumnNames(String sqlVerb, JdbcDatabaseManager.FactoryData data, StringBuilder sb)
      Appends column names to the given buffer in the format "A,B,C".
    • getFactory

    • getJDBCDatabaseManager

      @Deprecated public static JdbcDatabaseManager getJDBCDatabaseManager(String name, int bufferSize, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs)
      Creates a JDBC manager for use within the JdbcAppender, or returns a suitable one if it already exists.
      Parameters:
      name - The name of the manager, which should include connection details and hashed passwords where possible.
      bufferSize - The size of the log event buffer.
      connectionSource - The source for connections to the database.
      tableName - The name of the database table to insert log events into.
      columnConfigs - Configuration information about the log table columns.
      Returns:
      a new or existing JDBC manager as applicable.
    • getManager

      @Deprecated public static JdbcDatabaseManager getManager(String name, int bufferSize, Layout<? extends Serializable> layout, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings)
      Deprecated.
      Creates a JDBC manager for use within the JdbcAppender, or returns a suitable one if it already exists.
      Parameters:
      name - The name of the manager, which should include connection details and hashed passwords where possible.
      bufferSize - The size of the log event buffer.
      layout - The Appender-level layout
      connectionSource - The source for connections to the database.
      tableName - The name of the database table to insert log events into.
      columnConfigs - Configuration information about the log table columns.
      columnMappings - column mapping configuration (including type conversion).
      Returns:
      a new or existing JDBC manager as applicable.
    • getManager

      @Deprecated public static JdbcDatabaseManager getManager(String name, int bufferSize, Layout<? extends Serializable> layout, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings, boolean immediateFail, long reconnectIntervalMillis)
      Creates a JDBC manager for use within the JdbcAppender, or returns a suitable one if it already exists.
      Parameters:
      name - The name of the manager, which should include connection details and hashed passwords where possible.
      bufferSize - The size of the log event buffer.
      layout - the Appender-level layout
      connectionSource - The source for connections to the database.
      tableName - The name of the database table to insert log events into.
      columnConfigs - Configuration information about the log table columns.
      columnMappings - column mapping configuration (including type conversion).
      immediateFail - Whether to fail immediately with a AppenderLoggingException when connecting to JDBC fails.
      reconnectIntervalMillis - How often to reconnect to the database when a SQL exception is detected.
      Returns:
      a new or existing JDBC manager as applicable.
    • getManager

      public static JdbcDatabaseManager getManager(String name, int bufferSize, Layout<? extends Serializable> layout, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings, boolean immediateFail, long reconnectIntervalMillis, boolean truncateStrings)
      Creates a JDBC manager for use within the JdbcAppender, or returns a suitable one if it already exists.
      Parameters:
      name - The name of the manager, which should include connection details and hashed passwords where possible.
      bufferSize - The size of the log event buffer.
      layout - The Appender-level layout
      connectionSource - The source for connections to the database.
      tableName - The name of the database table to insert log events into.
      columnConfigs - Configuration information about the log table columns.
      columnMappings - column mapping configuration (including type conversion).
      immediateFail - Whether or not to fail immediately with a AppenderLoggingException when connecting to JDBC fails.
      reconnectIntervalMillis - How often to reconnect to the database when a SQL exception is detected.
      truncateStrings - Whether or not to truncate strings to match column metadata.
      Returns:
      a new or existing JDBC manager as applicable.
    • checkConnection

      private void checkConnection()
    • closeResources

      protected void closeResources(boolean logExceptions)
    • commitAndClose

      protected boolean commitAndClose()
      Description copied from class: AbstractDatabaseManager
      Commits any active transaction (if applicable) and disconnects from the database (returns the connection to the connection pool). With buffering enabled, this is called when flushing the buffer completes, after the last call to AbstractDatabaseManager.writeInternal(LogEvent). With buffering disabled, this is called immediately after every invocation of AbstractDatabaseManager.writeInternal(LogEvent).
      Specified by:
      commitAndClose in class AbstractDatabaseManager
      Returns:
      true if all resources were closed normally, false otherwise.
    • commitAndCloseAll

      private boolean commitAndCloseAll()
    • connectAndPrepare

      private void connectAndPrepare() throws SQLException
      Throws:
      SQLException
    • connectAndStart

      protected void connectAndStart()
      Description copied from class: AbstractDatabaseManager
      Connects to the database and starts a transaction (if applicable). With buffering enabled, this is called when flushing the buffer begins, before the first call to AbstractDatabaseManager.writeInternal(LogEvent). With buffering disabled, this is called immediately before every invocation of AbstractDatabaseManager.writeInternal(LogEvent).
      Specified by:
      connectAndStart in class AbstractDatabaseManager
    • createReconnector

      private JdbcDatabaseManager.Reconnector createReconnector()
    • createSqlSelect

      private String createSqlSelect()
    • fieldsToString

      private String fieldsToString()
    • getConnectionSource

      public ConnectionSource getConnectionSource()
    • getSqlStatement

      public String getSqlStatement()
    • getTableName

      public String getTableName()
    • initColumnMetaData

      private void initColumnMetaData() throws SQLException
      Throws:
      SQLException
    • isClosed

      private boolean isClosed(Statement statement) throws SQLException
      Checks if a statement is closed. A null statement is considered closed.
      Parameters:
      statement - The statement to check.
      Returns:
      true if a statement is closed, false if null.
      Throws:
      SQLException - if a database access error occurs
    • isClosed

      private boolean isClosed(Connection connection) throws SQLException
      Checks if a connection is closed. A null connection is considered closed.
      Parameters:
      connection - The connection to check.
      Returns:
      true if a connection is closed, false if null.
      Throws:
      SQLException - if a database access error occurs
    • reconnectOn

      private void reconnectOn(Exception exception)
    • setFields

      private void setFields(MapMessage<?,?> mapMessage) throws SQLException
      Throws:
      SQLException
    • setStatementObject

      private void setStatementObject(int j, String nameKey, Object value) throws SQLException
      Sets the given Object in the prepared statement. The value is truncated if needed.
      Throws:
      SQLException
    • shutdownInternal

      protected boolean shutdownInternal()
      Description copied from class: AbstractDatabaseManager
      Implementations should implement this method to perform any proprietary disconnection / shutdown operations. This method will never be called twice on the same instance, and it will only be called after AbstractDatabaseManager.startupInternal(). It is safe to throw any exceptions from this method. This method does not necessarily disconnect from the database for the same reasons outlined in AbstractDatabaseManager.startupInternal().
      Specified by:
      shutdownInternal in class AbstractDatabaseManager
      Returns:
      true if all resources were closed normally, false otherwise.
    • startupInternal

      protected void startupInternal() throws Exception
      Description copied from class: AbstractDatabaseManager
      Implementations should implement this method to perform any proprietary startup operations. This method will never be called twice on the same instance. It is safe to throw any exceptions from this method. This method does not necessarily connect to the database, as it is generally unreliable to connect once and use the same connection for hours.
      Specified by:
      startupInternal in class AbstractDatabaseManager
      Throws:
      Exception
    • truncate

      private Object truncate(String nameKey, Object value)
      Truncates the value if needed.
    • writeInternal

      protected void writeInternal(LogEvent event, Serializable serializable)
      Description copied from class: AbstractDatabaseManager
      Performs the actual writing of the event in an implementation-specific way. This method is called immediately from AbstractDatabaseManager.write(LogEvent, Serializable) if buffering is off, or from AbstractDatabaseManager.flush() if the buffer has reached its limit.
      Specified by:
      writeInternal in class AbstractDatabaseManager
      Parameters:
      event - The event to write to the database.
    • writeThrough

      protected void writeThrough(LogEvent event, Serializable serializable)
      Overrides:
      writeThrough in class AbstractDatabaseManager