Java 通过 Spring xml 数据源配置 postgresql 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34310641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:31:35  来源:igfitidea点击:

Configuring postgresql driver through Spring xml datasource

javasqlspringpostgresql

提问by Asoub

I've been trying to configure the connections made with a postgresql datasource declared in a xml Spring configuration file.

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

<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 shouldn't be using the DriverManagerDataSourceclass 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.

我知道,我不应该DriverManagerDataSource从 spring 开始使用这个类(我们很快就会转向 C3p0 或 DBCP),因为它不是真正的池化。我正在尝试设置 postgresql 连接的 socketTimeout 值(在此处描述https://jdbc.postgresql.org/documentation/head/connect.html)但当然,“socketTimeout”不是数据源的属性,所以它不起作用。

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

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

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

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.

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

采纳答案by Asoub

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:

谢谢 Deinum 先生,我找到了方法。实际上,即使知道该属性名为“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>

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

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