JDBC properties for setting rollback scope v17
If you're using a JDBC connector to connect to a client application, you use the autosave
and transaction_rollback_scope
properties together to specify the transaction rollback scope.
You can specify these properties in either the connection URL or as an
additional properties object parameter to DriverManager.getConnection
.
autosave
The autosave
parameter is a string that specifies what the driver does if a query containing
multiple statements fails. The possible values are: server
, always
, never
, and conservative
.
In
autosave=server
mode, JDBC relies on the server-side parametertransaction_rollback_scope
to save each statement by way of internal server savepoints before executing the next. The server rolls back to the previous statement if any statement in the query fails. If this parameter isn't supported on the server side, JDBC rejects the connection.In
autosave=always
mode, the JDBC driver first tries to use the server-sidetransaction_rollback_scope
property. If it isn't supported, then JDBC driver sets a savepoint before each query statement and rolls back to that savepoint in case of failure.In
autosave=never
mode (default), no savepoint activity is ever carried out. Inautosave=conservative
mode, savepoint is set for each query. However, the rollback is done only for rare cases like 'cached statement cannot change return type' or 'statement XXX is not valid', so JDBC driver rolls back and retries.
The default value for this property is never
.
This autosave=server
property is useful only
with the PostgreSQL server providing transaction_rollback_scope
functionality.
transaction_rollback_scope
The autosave
parameter is a string that determines the range of
operations that roll back when an SQL statement fails.
The default value is TRANSACTION
, which causes the entire transaction or
current subtransaction to roll back. This is the only mode
that you can select with the SET TRANSACTION
command.
You can specify the other possible mode, STATEMENT
, only during connection establishment, ALTER USER
, or ALTER DATABASE
. In that mode, only
the failed SQL
statement is rolled back, and the transaction is put back in normal mode.
autosave
test cases
Test cases for trying out values of the autosave
property
are available in the BatchAutoSaveTest.java
file. The following
SQL code shows the behavior that's expected when the
server provides transaction_rollback_scope
functionality and
autosave=server
is used on the JDBC side.
With autosave=server
, the following query inserts values (1)
, (3)
, and
(4)
and disregards the duplicate key violation
error:
The artifacts
directory contains the pgjdbc
jar file
postgresql-REL2Q.42.2.3.180601.jar
. This file needs to
be added to the CLASSPATH as usual. It also contains the
postgresql-REL2Q.42.2.3.180601-tests.jar
jar that can be used to test
the latest autosave
functionality.
You can test the BatchAutoSaveTest.java
file provided in the artifacts
as follows:
Export CLASSPATH to build and run the test case:
Compile the supplied test file:
Run the test (assuming user as
test
and running on localhost):
To modify the test cases, you can modify the BatchAutoSaveTest.java
file
in the artifacts
directory. Then compile and run the test cases.