Postgresql driver setup through Spring xml datasource

I tried to configure the connections created using the postgresql data source declared in the Spring xml configuration file.

<bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/dbname" />
        <property name="username" value="postgres" />
        <property name="password" value="" />
        <property name="socketTimeout" value="10"/>
    </bean>

I know I should not use a class DriverManagerDataSourcefrom spring, (we will move on to C3p0 or DBCP soon), because this is not a real pool. I am trying to set the socketTimeout value of a postgresql connection (described here https://jdbc.postgresql.org/documentation/head/connect.html ), but of course, "socketTimeout" is not a property of the data source, so it does not work.

Can this be done through datasource xml data configuration? Or should I do it somewhere else? Since the data source controls the connection, I don’t think I can do

props.setProperty("timeout",30);
Connection conn = DriverManager.getConnection(url, props);

DriverManagerDataSource? , , .

+4
1

, , . , "connectionProperties", ( , ?). :

<bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/dbname" />
        <property name="username" value="postgres" />
        <property name="password" value="" />
        <!--<property name="socketTimeout" value="10"/>-->

        <property name="connectionProperties">
            <props>
                <prop key="socketTimeout">10</prop>
            </props>
        </property>
   </bean>

- / , ;)

+6

Source: https://habr.com/ru/post/1620326/


All Articles