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:
java.io.Flushable
,java.lang.AutoCloseable
public final class JdbcDatabaseManager extends AbstractDatabaseManager
AnAbstractDatabaseManager
implementation for relational databases accessed via JDBC.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
JdbcDatabaseManager.FactoryData
Encapsulates data thatJdbcDatabaseManager.JdbcDatabaseManagerFactory
uses to create managers.private static class
JdbcDatabaseManager.JdbcDatabaseManagerFactory
Creates managers.private class
JdbcDatabaseManager.Reconnector
Handles reconnecting to JDBC once on a Thread.private static class
JdbcDatabaseManager.ResultSetColumnMetaData
-
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager
AbstractDatabaseManager.AbstractFactoryData
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ColumnConfig>
columnConfigs
private java.util.Map<java.lang.String,JdbcDatabaseManager.ResultSetColumnMetaData>
columnMetaData
private java.sql.Connection
connection
private JdbcDatabaseManager.FactoryData
factoryData
private static JdbcDatabaseManager.JdbcDatabaseManagerFactory
INSTANCE
private boolean
isBatchSupported
private JdbcDatabaseManager.Reconnector
reconnector
private java.lang.String
sqlStatement
private java.sql.PreparedStatement
statement
-
Fields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER
-
-
Constructor Summary
Constructors Modifier Constructor Description private
JdbcDatabaseManager(java.lang.String name, java.lang.String sqlStatement, java.util.List<ColumnConfig> columnConfigs, JdbcDatabaseManager.FactoryData factoryData)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static void
appendColumnName(int i, java.lang.String columnName, java.lang.StringBuilder sb)
private static void
appendColumnNames(java.lang.String sqlVerb, JdbcDatabaseManager.FactoryData data, java.lang.StringBuilder sb)
Appends column names to the given buffer in the format"A,B,C"
.private void
checkConnection()
protected void
closeResources(boolean logExceptions)
protected boolean
commitAndClose()
Commits any active transaction (if applicable) and disconnects from the database (returns the connection to the connection pool).private boolean
commitAndCloseAll()
private void
connectAndPrepare()
protected void
connectAndStart()
Connects to the database and starts a transaction (if applicable).private JdbcDatabaseManager.Reconnector
createReconnector()
private java.lang.String
createSqlSelect()
private java.lang.String
fieldsToString()
ConnectionSource
getConnectionSource()
private static JdbcDatabaseManager.JdbcDatabaseManagerFactory
getFactory()
static JdbcDatabaseManager
getJDBCDatabaseManager(java.lang.String name, int bufferSize, ConnectionSource connectionSource, java.lang.String tableName, ColumnConfig[] columnConfigs)
static JdbcDatabaseManager
getManager(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings)
Deprecated.static JdbcDatabaseManager
getManager(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.String tableName, ColumnConfig[] columnConfigs, ColumnMapping[] columnMappings, boolean immediateFail, long reconnectIntervalMillis)
static JdbcDatabaseManager
getManager(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.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.java.lang.String
getSqlStatement()
java.lang.String
getTableName()
private void
initColumnMetaData()
private boolean
isClosed(java.sql.Connection connection)
Checks if a connection is closed.private boolean
isClosed(java.sql.Statement statement)
Checks if a statement is closed.private void
reconnectOn(java.lang.Exception exception)
private void
setFields(MapMessage<?,?> mapMessage)
private void
setStatementObject(int j, java.lang.String nameKey, java.lang.Object value)
Sets the given Object in the prepared statement.protected boolean
shutdownInternal()
Implementations should implement this method to perform any proprietary disconnection / shutdown operations.protected void
startupInternal()
Implementations should implement this method to perform any proprietary startup operations.private java.lang.Object
truncate(java.lang.String nameKey, java.lang.Object value)
Truncates the value if needed.protected void
writeInternal(LogEvent event, java.io.Serializable serializable)
Performs the actual writing of the event in an implementation-specific way.protected void
writeThrough(LogEvent event, java.io.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, writeInternal
-
Methods 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 Detail
-
INSTANCE
private static final JdbcDatabaseManager.JdbcDatabaseManagerFactory INSTANCE
-
columnConfigs
private final java.util.List<ColumnConfig> columnConfigs
-
sqlStatement
private final java.lang.String sqlStatement
-
factoryData
private final JdbcDatabaseManager.FactoryData factoryData
-
connection
private volatile java.sql.Connection connection
-
statement
private volatile java.sql.PreparedStatement statement
-
reconnector
private volatile JdbcDatabaseManager.Reconnector reconnector
-
isBatchSupported
private volatile boolean isBatchSupported
-
columnMetaData
private volatile java.util.Map<java.lang.String,JdbcDatabaseManager.ResultSetColumnMetaData> columnMetaData
-
-
Constructor Detail
-
JdbcDatabaseManager
private JdbcDatabaseManager(java.lang.String name, java.lang.String sqlStatement, java.util.List<ColumnConfig> columnConfigs, JdbcDatabaseManager.FactoryData factoryData)
-
-
Method Detail
-
appendColumnName
private static void appendColumnName(int i, java.lang.String columnName, java.lang.StringBuilder sb)
-
appendColumnNames
private static void appendColumnNames(java.lang.String sqlVerb, JdbcDatabaseManager.FactoryData data, java.lang.StringBuilder sb)
Appends column names to the given buffer in the format"A,B,C"
.
-
getFactory
private static JdbcDatabaseManager.JdbcDatabaseManagerFactory getFactory()
-
getJDBCDatabaseManager
@Deprecated public static JdbcDatabaseManager getJDBCDatabaseManager(java.lang.String name, int bufferSize, ConnectionSource connectionSource, java.lang.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(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.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(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.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).reconnectIntervalMillis
-immediateFail
-- Returns:
- a new or existing JDBC manager as applicable.
-
getManager
public static JdbcDatabaseManager getManager(java.lang.String name, int bufferSize, Layout<? extends java.io.Serializable> layout, ConnectionSource connectionSource, java.lang.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 aAppenderLoggingException
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 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:
commitAndClose
in classAbstractDatabaseManager
- Returns:
- true if all resources were closed normally, false otherwise.
-
commitAndCloseAll
private boolean commitAndCloseAll()
-
connectAndPrepare
private void connectAndPrepare() throws java.sql.SQLException
- Throws:
java.sql.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 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:
connectAndStart
in classAbstractDatabaseManager
-
createReconnector
private JdbcDatabaseManager.Reconnector createReconnector()
-
createSqlSelect
private java.lang.String createSqlSelect()
-
fieldsToString
private java.lang.String fieldsToString()
-
getConnectionSource
public ConnectionSource getConnectionSource()
-
getSqlStatement
public java.lang.String getSqlStatement()
-
getTableName
public java.lang.String getTableName()
-
initColumnMetaData
private void initColumnMetaData() throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
isClosed
private boolean isClosed(java.sql.Statement statement) throws java.sql.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:
java.sql.SQLException
- if a database access error occurs
-
isClosed
private boolean isClosed(java.sql.Connection connection) throws java.sql.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:
java.sql.SQLException
- if a database access error occurs
-
reconnectOn
private void reconnectOn(java.lang.Exception exception)
-
setFields
private void setFields(MapMessage<?,?> mapMessage) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
setStatementObject
private void setStatementObject(int j, java.lang.String nameKey, java.lang.Object value) throws java.sql.SQLException
Sets the given Object in the prepared statement. The value is truncated if needed.- Throws:
java.sql.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 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:
shutdownInternal
in classAbstractDatabaseManager
- Returns:
- true if all resources were closed normally, false otherwise.
-
startupInternal
protected void startupInternal() throws java.lang.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 classAbstractDatabaseManager
- Throws:
java.lang.Exception
-
truncate
private java.lang.Object truncate(java.lang.String nameKey, java.lang.Object value)
Truncates the value if needed.
-
writeInternal
protected void writeInternal(LogEvent event, java.io.Serializable serializable)
Description copied from class:AbstractDatabaseManager
Performs 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:
writeInternal
in classAbstractDatabaseManager
- Parameters:
event
- The event to write to the database.
-
writeThrough
protected void writeThrough(LogEvent event, java.io.Serializable serializable)
- Overrides:
writeThrough
in classAbstractDatabaseManager
-
-