Reputation: 1300
I want to use the statement_timeout
connection parameter when I open a connection.
I have successfully tested this parameter via executing SET statement_timeout to 5000
as statement but I want it to be provided on opening the connection.
Currently I'm having the following methods (simplified):
public Connection openConnection() {
try {
Driver driver = (Driver) Class.forName("org.postgresql.Driver").newInstance();
return driver.connect("jdbc:postgresql://localhost:15432/postgres", getProperties());
} catch (Exception exp) {
throw new RuntimeException(String.format("Unable to connect to JDBC with additional connection properties.", exp);
}
}
private Properties getProperties() {
Properties properties = new Properties();
properties.put("statement_timeout", "5000");
return properties;
}
But even when querying SELECT pg_sleep(6)
(This query will take at least 6000ms) no exception occurs.
Is it even possible? Because on Chapter 3. Initializing the Driver I only found three other timeouts: loginTimeout
, connectTimeout
, socketTimeout
. Are they related somehow to mine?
Upvotes: 1
Views: 1816
Reputation: 246013
According to the documentation, you should use the options
parameter:
- options = String
Specify 'options' connection initialization parameter.
The value of this property may contain spaces or other special characters, and it should be properly encoded if provided in the connection URL. Spaces are considered to separate command-line arguments, unless escaped with a backslash (
\
);\\
represents a literal backslash.
The options
parameter this refers to is documented in the PostgreSQL documentation:
options
Specifies command-line options to send to the server at connection start. For example, setting this to
-c geqo=off
sets the session's value of thegeqo
parameter tooff
. Spaces within this string are considered to separate command-line arguments, unless escaped with a backslash (\
); write\\
to represent a literal backslash. For a detailed discussion of the available options, consult Chapter 19.
So you should set the options
parameter to
-cstatement_timeout=5000
Upvotes: 2