Java log4j2“GenericObjectPool不带参数”

时间:2017-01-10 10:47:10

标签: java jdbc log4j2

我遇到与用户相同的问题: log4j type GenericObjectPool does not take parameters但我一直无法找到这个问题的答案。 Log4j2在附加到滚动文件时有效,但此处列出的方法如下: https://logging.apache.org/log4j/2.x/manual/appenders.html给了我错误 “GenericObjectPool不接受参数”

package database;

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", "root");
  properties.setProperty("password", "tiger");

        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
        DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
    "jdbc:mysql://127.0.0.1/logging", 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();
 }
}

1 个答案:

答案 0 :(得分:0)

问题已由我自己解决。

我添加了maven依赖,它对我来说很好。

<dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.6</version>
</dependency>