java - Wrong JDBC driver being used? -
i have method inserts record postgres db , returns identity field generated said record. problem is, if include redshift driver in pom file, driver getting used instead of postgres driver - , redshift driver doesn't allow returning identity value.
the code is:
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 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 pom used, works:
<dependencies> <dependency> <groupid>org.postgresql</groupid> <artifactid>postgresql</artifactid> <version>9.4-1201-jdbc41</version> </dependency> </dependencies>
when pom used, 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 making java choose redshift driver rather postgres driver?
(the redshift driver's classpath com.amazon.jdbc41.driver, wouldn't think it's classpath conflict)
tia
your problem is, java supports serviceloader
mechanism jdbc 4.0.
in jdbc 4, drivermanager
find , register drivers meta-inf/services/java.sql.driver
setting in jar file. when call getconnection()
drivermanager choose first suitable driver jdbc url given.
now redshift , postgres driver different in terms of jdbc url, (quoting redshift docs http://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html#obtain-jdbc-url):
a jdbc url specified former format of jdbc:postgresql://endpoint:port/database still work.
now, happens, is, jdbc driver redshift loaded on service entry , places himself driver redshift jdbc url , postgres one.
i cannot tell if drivermanager
allows overwriting existing jdbc-driver-links loading driver, solution problem explicit control either load postgres driver first (if url registered once) or explicitly load after redshift driver (if jdbc url mapping can overwritten).
i cannot tell, if there property disallow redshift driver register postgres url.
Comments
Post a Comment