pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bill Moran <wmoran@potentialtech.com>
To: Sridhar N Bamandlapally <sridhar.bn1@gmail.com>
Cc: Dave Cramer <pg@fastcrypt.com>
Cc: Vitalii Tymchyshyn <vit@tym.im>
Cc: Craig Ringer <craig@2ndquadrant.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: John R Pierce <pierce@hogranch.com>
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: JDBC behaviour
Date: Sun, 21 Feb 2016 07:56:09 -0500
Message-ID: <20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com> (raw)
In-Reply-To: <CAGuFTBXPCWEUYq_e4nabgDu+cuM_sJxWt9QxQKgsd9D2SuA0hw@mail.gmail.com>
References: <VisenaEmail.10e.692062643617cb13.152f4695802@tc7-visena>
	<CAGuFTBXSwk2UmFs_b0wYuTv_BFtLwVmco1mgT5OSb+dwX-U+Sw@mail.gmail.com>
	<4C508581-9CA3-490D-A376-CC1F9FF5D258@me.com>
	<CAGuFTBW_xcn5nWtggiwq46f=6Rc5TYEqwtmiWEEGGfB+PSLbUg@mail.gmail.com>
	<CAMsr+YH3NHZ__xSxWfVH4neGa6w8nLRuLLRG8kqXt9bgE1eEBA@mail.gmail.com>
	<56C8487F.7020406@hogranch.com>
	<CAMsr+YF6nj0Qh+A0J3hJrQD1ofwq_=k_tXUewb0EQO_0uj7Azg@mail.gmail.com>
	<CAKFQuwbo6QZ7dae91mPcnXJyija4EfDX4=F_ULta2hXf4XwbEw@mail.gmail.com>
	<CABWW-d3Mu=vto2ML22Q+iQe-znNkQLyyew_FJmZn086oBLiqKA@mail.gmail.com>
	<20160220111625.4ee764661333e42e230348ad@potentialtech.com>
	<CABWW-d3dDrDQbKA1uh5o3XzFcLrzc7QtHkRsxSdrznUUD=Vq6Q@mail.gmail.com>
	<20160220120901.58bea0d780fad45775f04bfa@potentialtech.com>
	<CABWW-d3kagq=8EnXFRzc7Jzij08DNY0VbdT=ZcSa48rnuQXOYg@mail.gmail.com>
	<CADK3HHJ5PW1GBgRiy22UsXoCtJT_G=pY74nXpUAbVA6qiX27rA@mail.gmail.com>
	<CAGuFTBXPCWEUYq_e4nabgDu+cuM_sJxWt9QxQKgsd9D2SuA0hw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>

On Sun, 21 Feb 2016 07:50:19 +0530
Sridhar N Bamandlapally <sridhar.bn1@gmail.com> wrote:

> My expectation is simple, please refer below
> 
> create table employee(empid numeric(4) primary key, ename varchar(20));
> 
> from Java/jdbc code, conn.setAutoCommit(false)
> 
> insert into employee values(1, 'K1');
> insert into employee values(1, 'K1');
> insert into employee values(2, 'K2');
> 
> by looking at exceptions i may rollback or commit, i.e. conn.rollback() or
> conn.commit()
> if I rollback table should be empty,
> if I commit table should have 2 rows
> 
> is there any way is possible ?

Two other responses to this email are incorrect: turning on autocommit will
not allow you rollback the entire transaction (which I believe you needed)
and an the ON CONFLICT statement won't catch errors other than the empid
conflict, which I believe was an example and not the sum total of possible
errors you want to avoid.

Of course, if I'm misunderstanding those points, then those actually are
viable solutions.

However, I think what you're really looking for are savepoints, which will
give you the flexibility to handle just about any situation:

BEGIN TRANSACTION;
SAVEPOINT sp;
insert into employee values(1, 'K1');
RELEASE SAVEPOINT sp;
SAVEPOINT sp;
insert into employee values(1, 'K1');
ROLLBACK TO SAVEPOINT sp;
SAVEPOINT sp;
insert into employee values(2, 'K2');
RELEASE SAVEPOINT sp;
COMMIT TRANSACTION;

After each INSERT you have the option to RELEASE the savepoint (allowing
the insert to succeed) or ROLLBACK the savepoint (which rolls back only
to where the savepoint was created). Once all inserts have been attempted
you have the option to either COMMIT or ROLLBACK the entire transaction.
This is a generic solution that will work with any types of errors the
INSERTs may have. It's also fairly easy to abstract into your Java code
so the pattern can easily be reused.

Read the docs and experiment some until you're comfortable with the
concept:
http://www.postgresql.org/docs/9.5/static/sql-savepoint.html

-- 
Bill Moran


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



view thread (73+ messages)  latest in thread

Message-ID: <20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com>
Permalink:  ../20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com/
Also on:    postgresql.org/message-id/20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: wmoran@potentialtech.com, sridhar.bn1@gmail.com, pg@fastcrypt.com, vit@tym.im, craig@2ndquadrant.com, david.g.johnston@gmail.com, pierce@hogranch.com
  Subject: Re: JDBC behaviour
  In-Reply-To: <20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com>
  References: <VisenaEmail.10e.692062643617cb13.152f4695802@tc7-visena> <4C508581-9CA3-490D-A376-CC1F9FF5D258@me.com> <CAGuFTBW_xcn5nWtggiwq46f=6Rc5TYEqwtmiWEEGGfB+PSLbUg@mail.gmail.com> <CAMsr+YH3NHZ__xSxWfVH4neGa6w8nLRuLLRG8kqXt9bgE1eEBA@mail.gmail.com> <56C8487F.7020406@hogranch.com> <CAMsr+YF6nj0Qh+A0J3hJrQD1ofwq_=k_tXUewb0EQO_0uj7Azg@mail.gmail.com> <CAKFQuwbo6QZ7dae91mPcnXJyija4EfDX4=F_ULta2hXf4XwbEw@mail.gmail.com> <CABWW-d3Mu=vto2ML22Q+iQe-znNkQLyyew_FJmZn086oBLiqKA@mail.gmail.com> <20160220111625.4ee764661333e42e230348ad@potentialtech.com> <CABWW-d3dDrDQbKA1uh5o3XzFcLrzc7QtHkRsxSdrznUUD=Vq6Q@mail.gmail.com> <20160220120901.58bea0d780fad45775f04bfa@potentialtech.com> <CABWW-d3kagq=8EnXFRzc7Jzij08DNY0VbdT=ZcSa48rnuQXOYg@mail.gmail.com> <CADK3HHJ5PW1GBgRiy22UsXoCtJT_G=pY74nXpUAbVA6qiX27rA@mail.gmail.com> <CAGuFTBXPCWEUYq_e4nabgDu+cuM_sJxWt9QxQKgsd9D2SuA0hw@mail.gmail.com> <20160221075609.48ada8383aa4c5c9be17ad1e@potentialtech.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox