【发布时间】:2013-08-31 22:26:09
【问题描述】:
我有function 其中checks pid 的maximum value 然后inserts 进入数据库。
但是当我有concurrent transactions!时它会引发conflict
那么我该如何避免这种冲突!
create table tablea(
sno serial,
pid integer,
ppid integer,
pname text
);
--这是检查pid最大值然后插入数据库的函数。
CREATE OR REPLACE FUNCTION insert_details(_pname text)
RETURNS void AS
$BODY$
declare
_pid int;
begin
_pid := (select MAX(pid) from tablea);
_pid := coalesce(_pid, 0) + 1;
insert into tablea(pid,pname)
values(_pid,_pname);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
--(Sub Party)This is the function checks the maximum value of ppid and then inserts into the database.(sub-Party)
CREATE OR REPLACE FUNCTION insert_details(_pid int,_pname text)
RETURNS void AS
$BODY$
declare
_ppid int;
begin
_ppid := (select MAX(ppid) from tablea where pid=_pid);
_ppid := coalesce(_ppid, 0) + 1;
insert into tablea(pid,ppid,pname)
values (_pid,_ppid,_pname);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
我的要求:当我从前端点击submit 按钮时,我的field(pname) 应该插入tablea,通过增加pid by +1,所以我不能删除我的 pid 来自 tablea,并且 pid 保持整数,以便我可以通过相同的 pid 插入另一条记录,并且流程也与 ppid 相同..
所以我应该改变我的表结构或任何其他方式来纠正并发事务中的冲突。
这就是我的整体概念的全部内容 sql fiddle demo
【问题讨论】:
-
我使用 java Primefaces 作为 Springs 和休眠的用户界面。
标签: java postgresql transactions postgresql-9.1 postgresql-9.2