PostgreSQL
 SQL >> Datenbank >  >> RDS >> PostgreSQL

PostgreSQL-Verbindung aus IBM WSJdbc41Connection auspacken

Wenn Sie ein JDBC-Objekt (DataSource, Connection usw.) auf ein herstellerspezifisches Interface entpacken möchten, muss der im konfigurierten <dataSource> verwendete JDBC-Treiber für den Application-Classloader erreichbar sein. Die Konfiguration sieht etwa so aus:

<application location="oraclejdbcfat.war">
  <!-- die 'DBLib' mit dem JDBC-Treiber- JAR für den App-Classloader bereitstellen -->
  <classloader commonLibraryRef="DBLib"/>
</application>

<library id="DBLib">
  <fileset dir="${server.config.dir}/postgresql/" includes="*.jar"/>
</library>

<dataSource jndiName="jdbc/myDS">
  <jdbcDriver libraryRef="DBLib"/>
  <properties .../>
</dataSource>

Danach entpacken Sie das Objekt wie gewohnt:

DataSource ds = InitialContext.doLookup("jdbc/myDS");
Connection conn = ds.getConnection();
PGConnection pgConn = conn.unwrap(org.postgresql.PGConnection.class);

Es gibt ausserdem das boolesche Attribut enableConnectionCasting am <dataSource>, das das Unwrapping beim getConnection() automatisch übernimmt:

<dataSource jndiName="jdbc/myDS" enableConnectionCasting="true">

Damit wird der Java-Code etwas kürzer:

DataSource ds = InitialContext.lookup("jdbc/myDS");
PGConnection pgConn = (PGConnection) ds.getConnection();