HikariCP Pool makes Logback's insertFromJNDI configuration stop working - logback

I have two Spring MVC applications that share a commons.jar library. This library includes logback logging library (logback 1.2.3 and slf4j 1.7.25) and the logback.xml file.
Both wars include this line in their web.xml file:
<env-entry>
<env-entry-name>applicationName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>nameOfApplicationA|nameOfApplicationB</env-entry-value>
</env-entry>
Each application generates its own log file including hostname, for example: HOST1-nameOfApplicationA.log. Logback configuration is as follows:
<insertFromJNDI env-entry-name="java:comp/env/applicationName" as="APP_NAME" />
<appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${HOSTNAME}-${APP_NAME}.log</file>
...
</appender>
Everything was working OK (Spring MVC 4.3.7.RELEASE, Hibernate 4, C3P0 latest), but we decided to upgrade to Hibernate 5.2.10 and change to HikariCP 2.6.1. After that, logback was no longer able to resolve java:comp/env/applicationName:
ERROR in ch.qos.logback.classic.joran.action.InsertFromJNDIAction - [java:comp/env/applicationName] has null or empty value
Resulting in both applications using the same file name HOST1-APP_NAME_IS_UNDEFINED.log.
As we changed at the same time Hibernate and HikariCP we went back to C3P0 to check the root cause, and can confirm that the new version of Hibernate has nothing to do. The change was developed in its own branch so no other change seems to affect (anyway, when returning to C3P0 it works).
I've been doing some tracing in Hikari's and Logback's code but I'm not able to see anything. I'm stuck, no idea of what to look.
Plan B is insert in each war its own logback.xml but I would like to avoid it and understand the problem as it may affect other parts of the application.
Both wars are deployed together in an Apache Tomcat/8.0.38 server. Tried also 8.5.12. It also happens if only one of the wars is deployed alone.

Although I found no solution, #brettw identified the problem (see https://github.com/brettwooldridge/HikariCP/issues/873), and got a workaround.
It seems that because HikariCP depends on slf4j, and HikariCP is also being initialized and registered into JNDI, is that causing Logback to initialize before the <env-entry> entries have registered.
The test made was initalize Hikari datasource with "org.apache.naming.factory.BeanFactory" factory instead of "com.zaxxer.hikari.HikariJNDIFactory". This way it works correctly.

Related

Can log4jdbc be used with spring boot?

I have a Spring boot app I'm trying to add database logging to which is better than
spring.jpa.properties.hibernate.show_sql=true
log4jdbc, from
https://github.com/marcosemiao/log4jdbc
seems to be the most up to date fork around, seems to format nicely, fills in parameters and adds timing, exactly what I want.
But when I configure it as stated in the readme, changing
spring.datasource.url=jdbc:mysql://localhost:3306/coindatabase?useSSL=false
to
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false
something seems to not like my reference to mysql and seems to try to fall back to H2:
Caused by: java.lang.RuntimeException: Driver org.h2.Driver claims to not accept jdbcUrl, jdbc:log4jdbc:mysql://localhost:3306/coindatabase?useSSL=false
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106)
Is there some easy way to make this work together?
log4jdbc for spring boot wrapper:
<groupId>com.integralblue</groupId>
<artifactId>log4jdbc-spring-boot-starter</artifactId>
which seems to pull in the implementation from:
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
Additional info:
Don't modify the spring.datasource.url property in your Spring Boot application.properties file; leave the URL as previously defined to access your MYSQL instance.
Instead, after grabbing the com.integralblue maven target, simply set the logging level of choice (ex logging.level.jdbc.sqltiming=info) and your previously defined log4j log will have the DB stuff in it.
See here as was well
You need to use this library in your build.gradle:
// https://mvnrepository.com/artifact/com.integralblue/log4jdbc-spring-boot-starter
compile group: 'com.integralblue', name: 'log4jdbc-spring-boot-starter', version: '2.0.0'
If you get the warning:
"Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary."
you can set the correct Driver yourself via properties:
log4jdbc.drivers=com.mysql.cj.jdbc.Driver
log4jdbc.auto.load.popular.drivers=false
The documentation for configuration can be found on Github

wildfly10 and slf4j and logback do not work

I followed the following configuration for wildfly10:
https://developer.jboss.org/thread/237094
So to be able to use slf4j and logback in my application I disabled the logging subsystem:
<jboss-deployment-structure>
<deployment>
<!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
<!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</jboss-deployment-structure>
With this configuration my application uses correctly my logback configuration: the problem is that the server does not use anymore its own logging, so the general information are not written anymore to server.log, but they are written to my application appender.
This sounds very strange to me: I tried a lot of other configurations like excluding directly the modules (i.e. org.slf4j and org.slf4j.impl) on my jboss-deployment-structure.xml file but with no effect.

ClassNotFoundException: com.mysql.jdbc.Driver (jar already in buildpath) [duplicate]

Could someone provide a few details on how to configure Tomcat to access MySQL?
In which directory within Tomcat do I place mysql-connector-java-5.1.13-bin? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib?
Do I need to add configuration to context.xml or server.xml?
Should I create a web.xml file and place it under Tomcat 6.0\webapps\myapp\WEB-INF? If so, then what should the contents of this file look like?
1: Where to place mysql-connector-java-5.1.13-bin in Tomcat directory? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib?
That depends on where the connections are to be managed. Normally you would like to create a connection pooled JNDI datasource to improve connecting performance. In that case, Tomcat is managing the connections and need to have access to the JDBC driver. You should then drop the JAR file in Tomcat/lib.
But if you're doing it the basic way using DriverManager#getConnection(), then it in fact don't matter if you drop it in Tomcat/lib or YourApp/WEB-INF/lib. You however need to realize that the one in Tomcat/lib will apply for all deployed webapps and that the one in YourApp/WEB-INF/lib will override the one in Tomcat/lib for only the particular webapp.
2: Do I need to confirgure context.xml or server.xml files?
That depends on where the connections are to be managed. When using a JNDI datasource, it suffices to configure it using YourApp/META-INF/context.xml like follows (just create file if not exist):
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/yourdb" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/yourdb"
driverClassName="com.mysql.jdbc.Driver"
username="yourname" password="yourpass"
/>
</Context>
and the YourApp/WEB-INF/web.xml as follows:
<resource-env-ref>
<resource-env-ref-name>jdbc/yourdb</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
If you're doing it the basic DriverManager way, then it's all up to you. Hardcoded, properties file, XML file, etcetera. You should manage it youself. Tomcat won't (and can't) do anything useful for you.
Noted should be that the YourApp/META-INF/context.xml is specific to Tomcat and clones. Each servletcontainer/appserver has its own way of defining JNDI resources. In Glassfish for example, you'd like to do that through the webbased admin interface.
3: Should I write web.xml file and need to place under Tomcat 6.0\webapps\myapp\WEB-INF? If Yes, then what should be the contents of file?
You should always supply one. It's not only to configure resources, but also to define servlets, filters, listeners and that kind of mandatory stuff to run your webapp. This file is part of the standard Servlet API.
See also:
Is it safe to use a static java.sql.Connection instance in a multithreaded system?
How should I connect to JDBC database / datasource in a servlet based application?
Where do I have to place the JDBC driver for Tomcat's connection pool?
DAO Tutorial - basic JDBC/DAO tutorial, targeted on Tomcat/JSP/Servlet
The answer to your questions:
One option is what you've mentioned: place the driver under WEB-INF/lib directory in your WAR file. The other would be in $TOMCAT_HOME/lib directory. The advantage of the latter would be that you don't need to copy the connector jar into every single project you deploy on that application server. Disadvantage is you will need to remember to put the jar file in place before deploying your application in a different application server.
If you need to change something in the default configuration, yes. Otherwise, there are context.xml and server.xml files with default options shipped with tomcat installations.
Your application's (WAR) web.xml should be under WEB-INF directory in your deploy file. You can look at the accepted content to that file in Java EE's servlet container specification. Usually, you place your servlet, filter and their corresponding mappings in that file.

logback dosen't work in Weblogic12c

My Java EE 6 application uses slf4j with logback as logging framework. I have openjpa custom logging which is not working on Weblogic while it was ok on glassfish before (whit openjpa 1.2).
When I add my custom log factory to "openjpa.log" property in persistence.xml, weblogic ignores this and doesn't work.
my custom log factory:
<property name="openjpa.Log" value="com.kishware.core.log.openjpa.CustomSLF4JLogFactory"/>
Here is the weblogic console output when ignores the property:
<Aug 17, 2013 11:29:35 AM GMT+04:30> <Warning> <J2EE> <BEA-160202> <You have specified a openjpa.Log setting in your configuration for persistence unit banco-product#pu-channel-manager. This setting will be ignored and all log messages will be sent to the WebLogic Server logging subsystem. Trace-level logging is controlled by the various JPA-specific debug settings in config.xml, or through the WebLogic Server Administration Console.>
I should mention that I'm using JPA 2.1 with Toplink implementation.
I would be happy to get some hints, how this could be solved.
I should mention that I'm using JPA 2.1 with Toplink implementation
Right there is your problem. You're trying to configure Toplink (and I think you mean EclipseLink) with OpenJPA configuration properties.

NHibernate will insert but not update after move to host with shared server running mysql

I have a site running MVC and Nhibernate (not fluent) using standard session per request in an http module, runs fine locally (also with mysql) but after a move to a hosting provider no update statements are being issued.
I can insert but not update, no exceptions are raised, I have the 'show_sql' option switched on which locally shows the update statements being issued but on the server no update statements are logged.
I don't think NHProf is an option for me as I can only run asp.net apps on my shared server, are there any other methods of diagnosing NH issues like this ?
Anyone had a similar issue ?
Cheers,
A
The issue was that I had moved from my local dev environment with IIS5 to a shared server with IIS7, IIS7 has a different syntax for registering http modules so my NHibernate session module was not firing which caused the behaviour originally described.
To fix this problem I added the modules section in the web.config under system.web to system.webServer, you can add the validation validateIntegratedModeConfiguration="false" key to the system.webServer section which will allow your config to have the module registered under both sections so you can have the same config for IIS5/IIS7.
NHProf is an option for you!
You can have it log to a file, then pick that file up later. This is the log4net config you need:
<log4net>
<appender name="NHProfAppender"
type="HibernatingRhinos.Profiler.Appender.NHibernate.NHProfOfflineAppender,
HibernatingRhinos.Profiler.Appender" >
<file value="nhprof_output.nhprof" />
</appender>
<logger name="HibernatingRhinos.Profiler.Appender.NHibernate.NHProfAppender.Setup">
<appender-ref ref="NHProfAppender"/>
</logger>
</log4net>
Alternatively, if you don't have an NHProf license, you can log the NHibernate stuff to a file in order to see what's happening.