David Smith wrote:
Try java:/comp instead.  If that doesn't help, then we'll need more info:

- OS:
- where you got tomcat from  (tomcat.apache.org or third party repackage)
- Details of your setup like what's in tomcat's common/lib folder?
- Config (relevant parts of server.xml, the webapp's context.xml and
WEB-INF/web.xml)
- The code used to access JNDI if available:
Hi David,

What does help is to perform JNDI lookup not during deployment, but with first logging attempt. So I rather think, that Tomcat (I know, I use old version, but I have to) fills JNDI information after webapp deployment. Can anybody confirm that? But anyway, why does log4j initialization takes place before JNDI context is ready?

My setup is quite complicated, there're several webapps, Josso SSO (it requires its jars to be present in tomcat's common/lib), etc. It happens on Ubuntu Linux, Tomcat extracted from tarball from tomcat.apache.org.

My JNDI lookup code is correct (because it works, but not during deployment). Anyway, here's appender code:

public class DataSourceAppender extends AsyncAppender {
private DataSource dataSource;
   private String jndi;

   public String getJndi() {
       return jndi;
   }

   public void setJndi(String jndi) {
       this.jndi = jndi;
   }

   @Override
   public void append(LoggingEvent event) {
      String statement = getLayout().format(event);
      Connection c = getConnection();
      // perform logging, etc
   }

   @Override
   public void activateOptions() {
       super.activateOptions();
// don't know why, but I cannot perform lookup here. JNDI context is not available yet
//        lookupDataSource();
   }

   @Override
   protected Connection getConnection() {
       Connection c = null;
try {
           if (dataSource == null) {
               lookupDataSource();
           }
           if (dataSource != null) {
               c = dataSource.getConnection();
           }
       }
       catch (Exception e) {
System.err.println("Cannot retrieve connection from datasource");
           e.printStackTrace(System.err);
       }
return c;
   }

   private synchronized void lookupDataSource() {
       if (dataSource == null) {
           try {
               InitialContext ctx = new InitialContext();
               dataSource = (DataSource) ctx.lookup(jndi);
           }
           catch (Exception e) {
System.err.println("Cannot retrieve DataSource from JNDI " + jndi);
               e.printStackTrace(System.err);
           }
       }
}

}

And this is the way I configure this appender:

<appender name="JDBC" class="mypackage.DataSourceAppender">
   <param name="jndi" value="java:comp/env/jdbc/sir"/>
<layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="insert into error_log ...."/>
   </layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
         <param name="LevelMin" value="INFO" />
   </filter>
</appender>



--
Mikolaj Rydzewski <m...@ceti.pl>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to