I have developed an application which is used to log record into Database with log4j2.xml using JDBCAppender. I have followed the following instruction here :- http://self-learning-java-tutorial.blogspot.in/2015/10/log4j2-jdbcappender-write-log-messages.html
My DB ConnectionFactory class is as follows:-
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
public class ConnectionFactory {
private static interface Singleton {
final ConnectionFactory INSTANCE = new ConnectionFactory();
}
private final DataSource dataSource;
private ConnectionFactory() {
Properties properties = new Properties();
properties.setProperty("user", "sa");
properties.setProperty("password", "Login@123"); // or get properties from some configuration file
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
"jdbc:sqlserver://SERV1-PC\\SQLEXPRESS:1433;databaseName=Anirban;", properties
);
new PoolableConnectionFactory(
connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
);
this.dataSource = new PoolingDataSource(pool);
}
public static Connection getDatabaseConnection() throws SQLException {
return Singleton.INSTANCE.dataSource.getConnection();
}
}
and my log4j2.xml snippet is as follows:-
<Appenders>
<JDBC name="databaseAppender" tableName="LogTable">
<ConnectionFactory class="ConnectionFactory" method="getDatabaseConnection" />
<Column name="EVENT_DATE" isEventTimestamp="true" />
<Column name="LEVEL" pattern="%level" />
<Column name="LOGGER" pattern="%logger" />
<Column name="MESSAGE" pattern="%message" />
<Column name="THROWABLE" pattern="%ex{full}" />
</JDBC>
</Appenders>
<AsyncRoot level="INFO" >
<AppenderRef ref="databaseAppender" />
</AsyncRoot>
Now everything works fine and I can see the log in DB.
But when I export this project as a jar file in order to make it a common shared library jar and use it in other applications as dependency, I am encountered with ClassNotFoundException.
So, whenever I start the application that has this shared jar file as dependency, I get the following :-
ERROR java.lang.ClassNotFoundException: ConnectionFactory java.lang.ClassNotFoundException: ConnectionFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
I a not sure why this ConnectionFactory java class is not getting into the classpath in the shared jar, but works fine when this is run as an application mentioned above.
Please suggest, how to include the ConnectionFactory java class in classpath in the jar? So do I need to do an modification in my log4j2.xml?