无法获取Logback和MySQL连接

2022-04-18 00:00:00 logging java logback

我使用Logback和MySQL的方式如下:

<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
        <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <jdbcUrl>${jdbc.url}</jdbcUrl>
            <user>${jdbc.username}</user>
            <password>${jdbc.password}</password>
        </dataSource>

    </connectionSource>
</appender>

我得到的是:

    11:51:45,851 |-WARN in ch.qos.logback.core.db.DataSourceConnectionSource@4c1be833 - Could not get a connection to discover the dialect to use. java.sql.SQLException: Connections could not be acquired from the underlying database!
    at java.sql.SQLException: Connections could not be acquired from the underlying database!
    at  at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at  at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
    at  at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    at  at ch.qos.logback.core.db.DataSourceConnectionSource.getConnection(DataSourceConnectionSource.java:72)
    at  at ch.qos.logback.core.db.DataSourceConnectionSource.start(DataSourceConnectionSource.java:46)
    at  at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:167)
    at  at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:318)
    at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:197)
    at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:183)
    at  at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:147)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:133)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:96)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:55)
    at  at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
    at  at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
    at  at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
    at  at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:54)
    at  at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
    at  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
    at  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
    at  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
    at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
    at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
    at  at com.grroo.manager.impl.AmazonEmailManagerImpl.<init>(AmazonEmailManagerImpl.java:42)
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
    at  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
    at  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
    at  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    at  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at  at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
    at  at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    at  at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    at  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at  at java.lang.Thread.run(Thread.java:662)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at  at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
    at  at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
    at  at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    at  at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    at  ... 51 common frames omitted
11:51:45,851 |-WARN in ch.qos.logback.core.db.DataSourceConnectionSource@4c1be833 - Connection does not support GetGeneratedKey method and could not discover the dialect.
11:51:45,854 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:13 - RuntimeException in Action for tag [appender] java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
    at java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
    at  at ch.qos.logback.core.db.DBAppenderBase.start(DBAppenderBase.java:62)
    at  at ch.qos.logback.classic.db.DBAppender.start(DBAppender.java:96)
    at  at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:96)

出了什么问题? 我使用的是MySQL-Connector-5.0.08,数据库是MySQL 5.0.51

编辑:

我尝试这样添加<sqlDialect class="ch.qos.logback.core.db.dialect.MySQLDialect" />

<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <jdbcUrl>${jdbc.url}</jdbcUrl>
                <user>${jdbc.username}</user>
                <password>${jdbc.password}</password>
            </dataSource>

        </connectionSource>
                <sqlDialect class="ch.qos.logback.core.db.dialect.MySQLDialect" />`
    </appender>

但没有成功!


解决方案

问题是我错过了logback-examples-1.0.0.jar

相关文章