Class JdbcDatabaseManager
java.lang.Object
org.apache.logging.log4j.core.appender.AbstractManager
org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager
org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager
- All Implemented Interfaces:
Flushable,AutoCloseable
An
AbstractDatabaseManager implementation for relational databases accessed via JDBC.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classEncapsulates data thatJdbcDatabaseManager.JdbcDatabaseManagerFactoryuses to create managers.private static final classCreates managers.private final classHandles reconnecting to JDBC once on a Thread.private static final classNested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager
AbstractDatabaseManager.AbstractFactoryData -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ColumnConfig> private Connectionprivate final JdbcDatabaseManager.FactoryDataprivate static final JdbcDatabaseManager.JdbcDatabaseManagerFactoryprivate booleanprivate JdbcDatabaseManager.Reconnectorprivate final Stringprivate PreparedStatementFields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateJdbcDatabaseManager(String name, String sqlStatement, List<ColumnConfig> columnConfigs, JdbcDatabaseManager.FactoryData factoryData) -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendColumnName(int i, String columnName, StringBuilder sb) private static voidappendColumnNames(String sqlVerb, JdbcDatabaseManager.FactoryData data, StringBuilder sb) Appends column names to the given buffer in the format"A,B,C".private voidprotected voidcloseResources(boolean logExceptions) protected booleanCommits any active transaction (if applicable) and disconnects from the database (returns the connection to the connection pool).private booleanprivate voidprotected voidConnects to the database and starts a transaction (if applicable).private JdbcDatabaseManager.Reconnectorprivate Stringprivate Stringprivate static JdbcDatabaseManager.JdbcDatabaseManagerFactorystatic JdbcDatabaseManagergetJDBCDatabaseManager(String name, int bufferSize, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs) Deprecated.static JdbcDatabaseManagergetManager(String name, int bufferSize, Layout<? extends Serializable> layout, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings) Deprecated.static JdbcDatabaseManagergetManager(String name, int bufferSize, Layout<? extends Serializable> layout, ConnectionSource connectionSource, String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings, boolean immediateFail, long reconnectIntervalMillis) static JdbcDatabaseManagergetManager(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 theJdbcAppender, or returns a suitable one if it already exists.private voidprivate booleanisClosed(Connection connection) Checks if a connection is closed.private booleanChecks if a statement is closed.private voidreconnectOn(Exception exception) private voidsetFields(MapMessage<?, ?> mapMessage) private voidsetStatementObject(int j, String nameKey, Object value) Sets the given Object in the prepared statement.protected booleanImplementations should implement this method to perform any proprietary disconnection / shutdown operations.protected voidImplementations should implement this method to perform any proprietary startup operations.private ObjectTruncates the value if needed.protected voidwriteInternal(LogEvent event, Serializable serializable) Performs the actual writing of the event in an implementation-specific way.protected voidwriteThrough(LogEvent event, Serializable serializable) Methods inherited from class org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager
buffer, flush, getManager, isBuffered, isRunning, releaseSub, shutdown, startup, toString, write, write, writeInternalMethods inherited from class org.apache.logging.log4j.core.appender.AbstractManager
close, getContentFormat, getCount, getLoggerContext, getManager, getName, getStrSubstitutor, hasManager, log, logDebug, logError, logger, logWarn, narrow, release, stop, updateData
-
Field Details
-
INSTANCE
-
columnConfigs
-
sqlStatement
-
factoryData
-
connection
-
statement
-
reconnector
-
isBatchSupported
private volatile boolean isBatchSupported -
columnMetaData
-
-
Constructor Details
-
JdbcDatabaseManager
private JdbcDatabaseManager(String name, String sqlStatement, List<ColumnConfig> columnConfigs, JdbcDatabaseManager.FactoryData factoryData)
-
-
Method Details
-
appendColumnName
-
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) Deprecated.Creates a JDBC manager for use within theJdbcAppender, 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 theJdbcAppender, 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 layoutconnectionSource- 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) Deprecated.Creates a JDBC manager for use within theJdbcAppender, 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-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-reconnectIntervalMillis-- 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 theJdbcAppender, 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 layoutconnectionSource- 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 aAppenderLoggingExceptionwhen 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:AbstractDatabaseManagerCommits 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 toAbstractDatabaseManager.writeInternal(org.apache.logging.log4j.core.LogEvent). With buffering disabled, this is called immediately after every invocation ofAbstractDatabaseManager.writeInternal(org.apache.logging.log4j.core.LogEvent).- Specified by:
commitAndClosein classAbstractDatabaseManager- Returns:
- true if all resources were closed normally, false otherwise.
-
commitAndCloseAll
private boolean commitAndCloseAll() -
connectAndPrepare
- Throws:
SQLException
-
connectAndStart
protected void connectAndStart()Description copied from class:AbstractDatabaseManagerConnects to the database and starts a transaction (if applicable). With buffering enabled, this is called when flushing the buffer begins, before the first call toAbstractDatabaseManager.writeInternal(org.apache.logging.log4j.core.LogEvent). With buffering disabled, this is called immediately before every invocation ofAbstractDatabaseManager.writeInternal(org.apache.logging.log4j.core.LogEvent).- Specified by:
connectAndStartin classAbstractDatabaseManager
-
createReconnector
-
createSqlSelect
-
fieldsToString
-
getConnectionSource
-
getSqlStatement
-
getTableName
-
initColumnMetaData
- Throws:
SQLException
-
isClosed
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
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
-
setFields
- Throws:
SQLException
-
setStatementObject
Sets the given Object in the prepared statement. The value is truncated if needed.- Throws:
SQLException
-
shutdownInternal
protected boolean shutdownInternal()Description copied from class:AbstractDatabaseManagerImplementations 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 afterAbstractDatabaseManager.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 inAbstractDatabaseManager.startupInternal().- Specified by:
shutdownInternalin classAbstractDatabaseManager- Returns:
- true if all resources were closed normally, false otherwise.
-
startupInternal
Description copied from class:AbstractDatabaseManagerImplementations 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:
startupInternalin classAbstractDatabaseManager- Throws:
Exception
-
truncate
Truncates the value if needed. -
writeInternal
Description copied from class:AbstractDatabaseManagerPerforms the actual writing of the event in an implementation-specific way. This method is called immediately fromAbstractDatabaseManager.write(LogEvent, Serializable)if buffering is off, or fromAbstractDatabaseManager.flush()if the buffer has reached its limit.- Specified by:
writeInternalin classAbstractDatabaseManager- Parameters:
event- The event to write to the database.
-
writeThrough
- Overrides:
writeThroughin classAbstractDatabaseManager
-
getManager(String, int, Layout, ConnectionSource, String, ColumnConfig[], ColumnMapping[], boolean, long)