简体   繁体   中英

Searchpath with Postgres does not work with Spring/DBCP datasource

Postgres extensions are installed in public schema. Set the search path for the app-specific schema on the DBCP datasource the following way:

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://myhost:myport/${db.gos_app.database}?searchpath=mySchema,public;?ApplicationName=${app.name}"/>
    <property name="connectionProperties" value="currentSchema=mySchema;"/>
    <property name="username" value="user"/>
    <property name="password" value="pw"/>
    <property name="defaultAutoCommit" value="false"/>
    <property name="maxActive" value="6" />
</bean>

But somehow I cannot use extensions installed in this public schema without qualifying them like "public.hstore".

Found the solution - JDBC driver does not know the searchpath property in the URL. But this has been nowhere reported :-( Instead currentSchema is given to driver which is then mapped to native Postgres searchpath (thus probably overwriting the default one there with public included). Not intuitive!

So the solution looks like this:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://myhost:myport/${db.gos_app.database}?ApplicationName=${app.name}"/>
<property name="connectionProperties" value="currentSchema=mySchema,public;"/>
<property name="username" value="user"/>
<property name="password" value="pw"/>
<property name="defaultAutoCommit" value="false"/>
<property name="maxActive" value="6" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM