Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Redshift和Postgres JDBC驱动程序都可以拦截jdbc://postgresql 连接字符串_Java_Spring_Postgresql_Jdbc_Amazon Redshift - Fatal编程技术网

Java Redshift和Postgres JDBC驱动程序都可以拦截jdbc://postgresql 连接字符串

Java Redshift和Postgres JDBC驱动程序都可以拦截jdbc://postgresql 连接字符串,java,spring,postgresql,jdbc,amazon-redshift,Java,Spring,Postgresql,Jdbc,Amazon Redshift,我有一个我不太确定如何解决的问题:我有一个web应用程序(打包为war),客户端可以配置他们希望指向的数据库。我们支持PostgreSQL和Redshift(以及其他)。JDBC4驱动程序是自动加载的,这很好。问题是: 红移JDBC驱动程序似乎会响应jdbc://postgresql PostgreSQL之前的连接字符串。这会在连接到PostgreSQL数据库时导致JDBC错误 我在pom.xml中将驱动程序名“org.postgresql.driver”指定为数据源的驱动程序,但我不确定Spr

我有一个我不太确定如何解决的问题:我有一个web应用程序(打包为war),客户端可以配置他们希望指向的数据库。我们支持PostgreSQL和Redshift(以及其他)。JDBC4驱动程序是自动加载的,这很好。问题是:

红移JDBC驱动程序似乎会响应jdbc://postgresql PostgreSQL之前的连接字符串。这会在连接到PostgreSQL数据库时导致JDBC错误

我在pom.xml中将驱动程序名“org.postgresql.driver”指定为数据源的驱动程序,但我不确定SpringJDBC模板如何选择驱动程序(除非它选择第一个处理程序)


还有其他人遇到这种问题吗?

这是因为红移驱动程序注册为能够处理
jdbc:postgresql
jdbc:redshift
URL前缀

当Postgres和Redshift驱动程序从其JAR加载时,它们各自向DriverManager注册

drivermanger.getDriver()
DriverManager.getConnection()
中实现的逻辑是循环遍历每个驱动程序,并在驱动程序指示它能够处理给定URL时停止

如果Postgres驱动程序先注册,那么一切都会正常进行,因为Postgres驱动程序只尝试处理
jdbc:postgresql
。如果红移驱动程序设法首先注册,那么Postgres驱动程序将永远不会被使用

解决这个问题的唯一方法是添加:

static {
    // Put the redshift driver at the end so that it doesn't
    // conflict with postgres queries
    java.util.Enumeration<Driver> drivers =  DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d.getClass().getName().equals("com.amazon.redshift.jdbc41.Driver")) {
            try {
                DriverManager.deregisterDriver(d);
                DriverManager.registerDriver(d);
            } catch (SQLException e) {
                throw new RuntimeException("Could not deregister redshift driver");
            }
            break;
        }
    }
}
静态{
//将红移驱动程序放在末端,这样它就不会
//与postgres查询冲突
java.util.Enumeration drivers=DriverManager.getDrivers();
while(drivers.hasMoreElements()){
Driver d=drivers.nextElement();
if(d.getClass().getName().equals(“com.amazon.redshift.jdbc41.Driver”)){
试一试{
DriverManager.deregisterDriver(d);
登记的司机经理(d);
}捕获(SQLE异常){
抛出新的RuntimeException(“无法注销红移驱动程序”);
}
打破
}
}
}

另一个解决方案是为常规PostgreSQL连接向JDBC连接字符串添加“OpenSourceSubtoColoverRide=true”

例如:

jdbc:postgresql://localhost:5432/postgres?OpenSourceSubProtocolOverride=true  

太糟糕了,在解决这个问题之前,我没有看到其他答案,但我通过这样做覆盖了红移驱动程序中的“postgres”子脚本,解决了这个问题

String[]name=com.amazon.redshift.PGInfo.PG_subtocol_name;
for(int i=0;i
我们使用的是Spring JDBC模板,有没有办法为数据源指定应该使用的驱动程序?我想我在驱动程序管理器上看到了类似于setDriver()的东西,但我有点像Spring noob…我不熟悉Spring,所以不确定。如果spring只是在幕后使用DriverManager,那么我认为重新排序驱动程序应该可以解决这个问题。这对我来说是一个固定的问题,将现有URL添加到PG db中。相关文档:接受这个答案,因为该设置专门用于处理JDBC URL冲突。