-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JdbcAppender using DataSourceConnectionSource could not append log event in embedded WAS #3128
Comments
Hi @minseo300,
We discussed the usage of JDBC connection pools provided by Spring Boot in #2807.
Therefore I would recommend you to use a different kind of connection source. The choice depends on the kind of connection pool you are using. Apache Commons DBCPIf your application uses the Apache Commons DBCP connection pool, add <PoolingDriver poolName="logging"
driverClassName="${spring:spring.datasource.driver-class-name}"
connectionString="${spring:spring.datasource.url}"
userName="${spring:spring.datasource.username}"
password="${spring:spring.datasource.password}"/> HikariCPIf your application uses the default HikariCP connection pool you can use the public class LoggingDataSource {
public static DataSource newInstance() {
PropertiesUtil environment = PropertiesUtil.getProperties();
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(environment.getStringProperty("spring.datasource.driver-class-name"));
dataSource.setJdbcUrl(environment.getStringProperty("spring.datasource.url"));
dataSource.setUsername(environment.getStringProperty("spring.datasource.username"));
dataSource.setPassword(environment.getStringProperty("spring.datasource.password"));
// Only one connection at a time is needed
dataSource.setMinimumIdle(0);
dataSource.setMaximumPoolSize(1);
return dataSource;
}
} Then you can use a connection source like: <ConnectionFactory class="eu.copernik.spring.boot.LoggingDataSource"
method="newInstance"/> Warning The above example is not complete, since you need to ensure that the |
@ppkarwasz In #2807, it seems you guys conclude there are more cons than pros to provide DataSourceConnectionSource in embedded WAS. DataSourceConnectionSource is supported to bind DataSource(defined at external source) using full JNDI path. In Spring boot application, we can define JNDI resource and using it with the JNDI path. And I think there are no differences between using JNDI in embedded WAS and external WAS. If we want to use external resource, define it and just bind it to where we want to use the resource. That's all we have to do. So there's a point. There are no differences the way to use between using JNDI in embedded WAS and external WAS. The reason why I can't use DataSourceConnectionSource in Spring boot application is when DataSourceConnectionSource try to look up JNDI resource, the JNDI resource is not initialized yet, so ConnectionSource is null but JdbcAppender requires that ConnectionSource is not null when initialize log4j2 logger context. In my opinion, If JdbcAppender allows that ConnectionSource to be null so log4j2 logger context initialize by log4j2.xml successfully(it means JdbcAppender doesn't care JNDI resource is initialized or not) and look up DataSource when JdbcAppender try to append log event to database, then we can use JNDI in Spring boot application. But in this case, if user(who use log4j2) writes wrong JNDI path, then look up will be failed when JdbcAppender try to append log event to database. It means user will recognize they wrote wrong JNDI path after log4j2 configuration is initialized successfully. It could be Spring boot application is started successfully. If you guys think that user recognize their mistake in log4j2.xml(or everything about configuring log4j2 configuration) after application is started successfully is a big problem, I can accept that opinion. Just this issue is from there's no guide in log4j2 JdbcAppender documentation about using DataSourceConnectionSource in Spring boot application. |
Description
I'm trying to configure JdbcAppender using DataSourceConnectionSource(jndi) in embedded Tomcat(I create application with Springboot)
I defined data source in application.yml, and using jndi-name in log4j2.xml.
[log4j2.xml]
[jndi configuration class]
As I analyzed JdbcAppender class, ConnectionSource in JdbcAppender is required, so if ConnectionSource is null, the exception is thrown while configuring log4j2 configuration.
In Spring boot application, log4j2 configuration is configured before embedded Tomcat loading jndi resource, so JdbcAppender isn't created and unable to locate appender for logger config.
Is there any plan to support log4j2 JdbcAppender with DataSourceConnectionSource with embedded WAS?
Configuration
**Version: 2.17.1
**Operating system: any
**JDK: 1.8
Logs
Reproduction
[An isolated test reproducing the test.
JUnit tests similar to the ones in the code base are extremely appreciated.]
The text was updated successfully, but these errors were encountered: