Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrent process inserting data in database
    primarykey
    data
    text
    <p>Consider following schema in postgres database.</p> <pre><code>CREATE TABLE employee ( id_employee serial NOT NULL PrimarKey, tx_email_address text NOT NULL Unique, tx_passwd character varying(256) ) </code></pre> <p>I have a java class which does following</p> <pre><code>conn.setAutoComit(false); ResultSet rs = stmt.("select * from employee where tx_email_address = 'test1'"); if (!rs.next()) { Insert Into employee Values ('test1', 'test1'); } ResultSet rs = stmt.("select * from employee where tx_email_address = 'test2'"); if (!rs.next()) { Insert Into employee Values ('test2', 'test2'); } ResultSet rs = stmt.("select * from employee where tx_email_address = 'test3'"); if (!rs.next()) { Insert Into employee Values ('test3', 'test3'); } ResultSet rs = stmt.("select * from employee where tx_email_address = 'test4'"); if (!rs.next()) { Insert Into employee Values ('test4', 'test4'); } conn.commit(); conn.setAutoComit(true); </code></pre> <p>The problem here is if there are two or more concurrent instance of the above mentioned transaction trying to write data. Only one transaction would eventually succeeds and rest would throw SQLException "unique key constraint violation". How do we get around this.</p> <p>PS: I have chosen only one table and simple insert queries to demonstrate the problem. My application is java based application whose sole purpose is to write data to the target database. and there can be concurrent process doing so and there is very high probability that some process might be trying to write in same data(as shown in example above).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload