Spring& jOOQ:配置postgreSQL驱动程序会引发错误

时间:2016-10-18 13:01:47

标签: xml spring postgresql driver jooq

我正在关注这些教程:tutorial 1tutorial 2
我制作了xml文件并根据此更改了.properties文件。现在我的应用程序无法启动,因为它无法使用我指定的驱动程序。我从Postgres Maven依赖项获得了驱动程序(如org.postgresql.Driver)。

为什么我会收到我的驱动程序错误的错误?

这是我在尝试启动应用程序时遇到的错误:

        

申请失败

           

说明

     

无法确定数据库类型为NONE的嵌入式数据库驱动程序类

     

动作:

     

如果您想要一个嵌入式数据库,请将支持的数据库放在   类路径。如果您要从a加载数据库设置   您可能需要激活它的特定配置文件(没有配置文件   目前有效。)

当我从教程2(QueryTest)运行测试时,我得到了这个:

14:39:38.380 [main] DEBUG org.jooq.tools.LoggerListener - Exception                
org.springframework.jdbc.UncategorizedSQLException: jOOQ; uncategorized SQLException for SQL [select "u"."id", "u"."name", "u"."password" from "public"."user" as "u"]; SQL state [null]; error code [0]; Cannot load JDBC driver class '${db.driver}'; nested exception is java.sql.SQLException: Cannot load JDBC driver class '${db.driver}'
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at nl.nazza.mediator.deviceconfigurator.springtransaction.ExceptionTranslator.exception(ExceptionTranslator.java:40)
    at org.jooq.impl.ExecuteListeners.exception(ExecuteListeners.java:244)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:363)
    at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:312)
    at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2705)
    at nl.nazza.mediator.deviceconfigurator.QueryTest.testJoin(QueryTest.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLException: Cannot load JDBC driver class '${db.driver}'
    at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2139)
    at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2033)
    at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:225)
    at com.sun.proxy.$Proxy18.prepareStatement(Unknown Source)
    at org.jooq.impl.ProviderEnabledConnection.prepareStatement(ProviderEnabledConnection.java:112)
    at org.jooq.impl.SettingsEnabledConnection.prepareStatement(SettingsEnabledConnection.java:76)
    at org.jooq.impl.AbstractResultQuery.prepare(AbstractResultQuery.java:231)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:320)
    ... 32 common frames omitted
Caused by: java.lang.ClassNotFoundException: ${db.driver}
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2131)
    ... 41 common frames omitted

这是在jooq-spring.xml

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" >
       <property name="url" value="${db.url}" />
       <property name="driverClassName" value="${db.driver}" />
       <property name="username" value="${db.username}" />
       <property name="password" value="${db.password}" />
    </bean>

这是device-configurator.properties

#Database Configuration
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/nazza_mediator
db.username=postgres
db.password=postgres

#jOOQ Configuration
jooq.sql.dialect=POSTGRES

2 个答案:

答案 0 :(得分:0)

我在XML中添加了以下内容:

<context:property-placeholder location="classpath:device-configurator.properties" ignore-resource-not-found="false"/>

我遇到了越来越多奇怪的错误,所以我更密切地关注https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/并使用了BoneCP而不是dbcp2。经过一些调整,它就像一个魅力。

这是我的xml文件最终的样子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

    <context:component-scan base-package="nl.nazza.mediator.deviceconfigurator"/>
    <context:property-placeholder location="classpath:device-configurator.properties" ignore-resource-not-found="false"/>

    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <bean class="org.jooq.impl.DataSourceConnectionProvider" name="connectionProvider">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="dslContext" class="org.jooq.impl.DefaultDSLContext">
        <constructor-arg ref="config" />
    </bean>
    <bean class="org.jooq.impl.DefaultConfiguration" name="config">
        <constructor-arg index="0" ref="connectionProvider" />
        <constructor-arg index="1"><null /></constructor-arg>
        <constructor-arg index="2"><null /></constructor-arg>
        <constructor-arg index="3"><null /></constructor-arg>
        <constructor-arg index="4"><null /></constructor-arg>
        <constructor-arg index="5"><value type="org.jooq.SQLDialect">${jooq.sql.dialect}</value></constructor-arg>
        <constructor-arg index="6"><null /></constructor-arg>
        <constructor-arg index="7"><null /></constructor-arg>
    </bean>
</beans>

答案 1 :(得分:0)

BoneCP作者在这里。你不应该再使用BoneCP了,它已经被推荐了很长一段时间了。请考虑迁移到HikariCP。