Log4J JDBCAppender
Hi!
I'm extending the org.apache.log4j.jdbc.JDBCAppender to log my web application directly in a database.
OK, this is my code:
package dbweb.logging;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
publicclass JDBCAppenderextends org.apache.log4j.jdbc.JDBCAppender{
private InitialContext context;
private ResourceBundle webappconf;
private String admindb;
private DataSource datasource;
public JDBCAppender(){
super();
try{
context =new InitialContext();
// Get ConnectionPool
datasource = (DataSource) context.lookup("comp/env/jdbc/MyConnectionPool");
}catch (javax.naming.NamingException nEx){
System.out.println(nEx.getExplanation());
}
}
protected Connection getConnection(){
try{
connection = datasource.getConnection();
connection.getAutoCommit();
return connection;
}catch (SQLException sqlEx){
System.out.println(sqlEx.getMessage());
returnnull;
}
}
protectedvoid closeConnection(Connection con){
try{
con.close();
}catch (SQLException sqlEx){
System.out.println(sqlEx.getMessage());
}
}
protected String getLogStatement(org.apache.log4j.spi.LoggingEvent event){
// MAKE STATEMENT
// ...
return sqlStatement;
}
}
When I try to get my web application ConnectionPool it seems that the JDBCAppender InitialContex isn't the web application one, in fact this command:
datasource = (DataSource) context.lookup("comp/env/jdbc/MyConnectionPool");
returns null, therefore all my application crashes when I try to log a message. This is the error:
No object bound to name java comp/env/jdbc/MyConnectionPool
How can I do to get my web application Connection Pool?
Thanks in advance.
+ Anhur +

