方法org.postgresql.jdbc4.Jdbc4Connection.isValid(int)尚未实现

时间:2014-08-31 17:03:09

标签: postgresql jdbc apache-commons-dbcp

我正在尝试使用postgres 9.1配置dbcp2

当我运行我的应用程序时,它会抛出异常:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented.)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:575)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:639)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:664)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:704)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:195)
    at com.springinpractice.ch02.service.impl.ContactServiceImpl.getContact(ContactServiceImpl.java:64)
    at com.springinpractice.ch02.ConsoleApp.main(ConsoleApp.java:16)
Caused by: java.sql.SQLException: Cannot create PoolableConnectionFactory (Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented.)
    at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2152)
    at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1903)
    at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 7 more
Caused by: org.postgresql.util.PSQLException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented.
    at org.postgresql.Driver.notImplemented(Driver.java:753)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.isValid(AbstractJdbc4Connection.java:109)
    at org.postgresql.jdbc4.Jdbc4Connection.isValid(Jdbc4Connection.java:21)
    at org.apache.commons.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:914)
    at org.apache.commons.dbcp2.PoolableConnection.validate(PoolableConnection.java:227)
    at org.apache.commons.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:303)
    at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2165)
    at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2148)
    ... 11 more

这是我的maven POM:

<dependencies>
    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <scope>runtime</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>


    <!-- Test Artifacts -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-framework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- For JDBC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>

    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

</dependencies>

10 个答案:

答案 0 :(得分:19)

该方法在当前的驱动程序版本中实现。你必须使用旧的PgJDBC。升级。它完全向后兼容。 (您应该在问题中指定您的PgJDBC版本。)

另外,依靠连接“验证”通常是一个坏主意。这只是一种尝试不完全隐藏竞争条件的方法。只需抓住连接并使用它即可。如果出现问题,您的应用程序应该捕获结果异常,检查SQLSTATE以查看它是否与连接有关,并使用新连接重试。

答案 1 :(得分:17)

用pom.xml中的以下条目替换postgresql。我的Postgress版本是postgresql-9.3.10-3。

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.3-1100-jdbc41</version>
</dependency>

答案 2 :(得分:7)

克雷格有权利。根本原因是postgresql组ID更改为org.postgresql,而旧组未获得更新。您应该刷新您的依赖项节点,即:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.5</version>
</dependency>

根据当前最新版本https://mvnrepository.com/artifact/org.postgresql/postgresql/

答案 3 :(得分:2)

尝试以下依赖性: 这样可以解决这个问题

     <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.14</version>
    </dependency>

答案 4 :(得分:1)

解决方案: 为参数“validationQuery”设置验证查询。

对于bean来说:   property name =“validationQuery”value =“SELECT 1”

在返回连接之前,将针对连接运行此验证查询。所以有一个有效的查询,上面是一个虚拟的查询。

https://commons.apache.org/proper/commons-dbcp/configuration.html

答案 5 :(得分:1)

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1100-jdbc41</version>
    </dependency>

更换了驱动程序,它可以正常工作!

答案 6 :(得分:1)

将POM.xml版本替换为

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1200-jdbc41</version>
</dependency>

此外,如果您使用的是SSL,则jdbc URL应采用以下格式

spring.datasource.url=jdbc:postgresql://hostName:5432/db?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

答案 7 :(得分:0)

此票帮助我解决了这个问题:Caused by: java.lang.NoSuchMethodError: org.postgresql.core.BaseConnection.getEncoding()Lorg/postgresql/core/Encoding;

基本上,您需要排除PostGIS存根:

 <dependency>
   <groupId>org.postgis</groupId>
   <artifactId>postgis-jdbc</artifactId>
   <version>1.3.3</version>
   <scope>compile</scope>
   <exclusions>
      <exclusion>
         <groupId>org.postgis</groupId>
         <artifactId>postgis-stubs</artifactId>
      </exclusion>
   </exclusions>
</dependency>

答案 8 :(得分:0)

Maven Central在postgresql下有一个旧版本的驱动程序。检查org.postgresql

答案 9 :(得分:0)

intelliJ安全定义(解析为9.4.1208-jdbc42-atlassian托管)

<dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>RELEASE</version>
</dependency>

OR:

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5.jre7</version>
        </dependency>