通过Spring xml数据源配置postgresql驱动程序 [英] Configuring postgresql driver through Spring xml datasource

查看:749
本文介绍了通过Spring xml数据源配置postgresql驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试配置使用xml Spring配置文件中声明的postgresql数据源建立的连接。

I've been trying to configure the connections made with a postgresql datasource declared in a xml Spring 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>

我知道,我不应该使用 DriverManagerDataSource 类从春季开始,(我们很快将移至C3p0或DBCP),因为它不是真正的池。
我正在尝试设置postgresql连接的socketTimeout值(在此处 https中进行描述://jdbc.postgresql.org/documentation/head/connect.html ),但是当然, socketTimeout不是数据源的属性,因此它不起作用。

I know, I shouldn't be using the DriverManagerDataSource class from spring, (we will soon move to C3p0 or DBCP) because it's not a real pooling. I'm 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 datasource, so it doesn't work.

是否可以通过数据源xml的配置来做到这一点?还是我应该在其他地方做?因为数据源管理连接,所以我认为我无法执行

Is it possible to do this through the datasource xml's configuration ? Or should I do it somewhere else ? Because the data source manages the connection I don't think I'll be able to do a

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

我什至可以使用DriverManagerDataSource做到这一点吗?我尝试搜索,但是没有找到任何有用的东西,因为没有很多人真正使用它。

Can I even do this with the DriverManagerDataSource ? I tried to search, but I didn't find anything usefull, as not a lot of people are really using it.

推荐答案

谢谢Deinum先生,我能够找到方法。
实际上,即使知道该属性的名称为 connectionProperties,我也没有找到很多答案(也许人们很少以这种方式使用它?)。因此,我将其发布:

Thank you M. Deinum, I was able to find how. Actually, even knowing the property was named "connectionProperties", I didn't found a lot answers (maybe people rarely use it this way ?). So I'm posting it:

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

如果有人有更好/更完整的答案,我会检查出来;)

If anyone has a better/more complete answer, I'll check it out ;)

这篇关于通过Spring xml数据源配置postgresql驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆