Wrong JDBC driver used?

I have a method that inserts a record into Postgres DB and returns the identification field generated for the specified record. The problem is that if I included the Redshift driver in my POM file, this driver will be used instead of the Postgres driver, and the Redshift driver will not allow to return the identifier value.

The code:

try {
  Class.forName( "org.postgresql.Driver" ).newInstance();
  Connection connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5433/postgres", "postgres", "password" );
  Statement stmt = connection.createStatement();
  stmt.execute( "insert into public.job ( job_name ) values ( 'test' )" , Statement.RETURN_GENERATED_KEYS );
  ResultSet keyset = stmt.getGeneratedKeys();
  if ( keyset.next() ) System.out.println( keyset.getLong( 1 ) );
}
catch ( Exception e ) {
  e.printStackTrace();
}

When this POM is used, it works:

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

When this POM is used, it does not work:

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>redshift.jdbc</artifactId>
        <version>1.1.2.0002</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>
</dependencies>

What does Java choose for the Redshift driver and not the Postgres driver?

(Redshift driver class path com.amazon.jdbc41.Driver , so I don't think this is a class conflict)

TIA

+4
source share
3 answers

, Java ServiceLoader JDBC 4.0.

JDBC 4 DriverManager META-INF/services/java.sql.Driver jar. getConnection(), DriverManager URL- jdbc.

redshift postgres URL- jdbc, ( redshift http://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html#obtain-jdbc-url):

URL JDBC, jdbc: postgresql://endpoint: / .

, , , JDBC URL- jdbc postgres.

, DriverManager jdbc--, , , postgres ( URL ) ( URL JDBC ).

, , URL- postgres.

+2

, , , :

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;
        }
    }
}

, .

+3

, PGDatasource DriverManger, org.postgresql.jdbc4.Jdbc4Connection:

Properties info = new Properties();
info.put("password", "postgres");
Jdbc4Connection c = new Jdbc4Connection(new HostSpec[]{new HostSpec("localhost", 5432)}, "postgres", "postgres", info, "");
0
source

All Articles