[firebird-support] Re: Insert ot update statements locks a table and all other instances that try to run same statement are lcoked to

2014-09-21 Thread Dmitry Yemanov dim...@users.sourceforge.net [firebird-support]
21.09.2014 20:46, Mark Rotteveel wrote:

 I don't know enough about reading below The Firebird stacktraces, but it
 looks like apart for process 14830 that they are all waiting for a lock.

More precisely, they're waiting for active transaction # 17348466 that 
blocks concurrent inserts into some index.

Selecting from MON$TRANSACTIONS may give an idea when and by whom that 
stuck transaction was started.


Dmitry




Odp: [firebird-support] Re: Insert ot update statements locks a table and all other instances that try to run same statement are lcoked to

2014-09-07 Thread 'liviusliv...@poczta.onet.pl' liviusliv...@poczta.onet.pl [firebird-support]
Hi,

Your assumption is wrong that logic is ok. You have problem with one 
installation probably because this client have havy load but others not.
Insert can only couse problem with unique constraint or reference to other 
tables or trigger on that record

Regards,
Karol Bieniaszewski

- Reply message -
Od: doyc...@dsoft-bg.com [firebird-support] firebird-support@yahoogroups.com
Do: firebird-support@yahoogroups.com
Temat: [firebird-support] Re: Insert ot update statements locks a table and all 
other instances that try to run same statement are lcoked to
Data: sob., wrz 6, 2014 21:12
Thank you for the idea but this is not what I'm looking for. 

I think that inserting same record twice must produce error and not to to block 
the execution of other connections. 

So in this case I'm trying to find where the problem is exactly.

This table is having problem with only one installation.

Another installation is having problem with another table. 

I really don't think is a good idea to make changes to the logic that works 
perfectly on 20 other installations just because I have problem that is not in 
the logic with only one installation.

Is there any way to look in the firebird process where it is stuck?

I'll try to extract stack trace from java side.







[firebird-support] Re: Insert ot update statements locks a table and all other instances that try to run same statement are lcoked to

2014-09-06 Thread Svein Erling Tysvær svein.erling.tysv...@kreftregisteret.no [firebird-support]
CASHBOXID is already know. It is a reference to a table that we don't touch in 
the transaction.
This table contains the current total amount of a cash box at the begging of 
the day.
So when I try to load data for a cash box a java code checks is there record 
for this cash box for current day. 
If there is no such record it calculates the total current amount and calls 
the insert that adds a record to this table.

Usually inserting record with same value for primary key will create error and 
probably will not  block the execution of the statement.
User is sysdba. There is no security restrictions for access to database.
End users have no access to database server at all. They all use code that 
runs in application server to access or modify data. 
That is why I'm not restricting the access to the database for the moment.

I'm not sure it is a lock conflict. But I suspect that something is locked 
somewhere and that forces the execution of the 
statement to stop and to lock all other inserts that other connections try to 
execute.

As I said I need ideas or hints what to do in order to find the real problem 
when this situation happens again.
One more thing. When I try to stop application server when this happens all 
connections that are blocked including the initial one stop the shutdown of 
the server.
When I try to kill firebird process that initially blocked the execution all 
other processes are unlocked and server is shutdown after that.
But in this case I try guess which process is that. When I look in the 
monitoring tables I can't see the pid of the process of each statement.

Have you considered trying something like (written using a text editor, so 
there could well be errors):

CREATE GENERATOR PK_GEN;
CREATE TABLE CASH_CASHBOX_DAY_AMMOUNT 
( PK INTEGER NOT NULL,
  CASHBOXID  INTEGER NOT NULL,
  DATE_TIME  NUMERIC( 18, 0) NOT NULL,
  AMMOUNTNUMERIC( 18, 0),
 CONSTRAINT PK_CASH_CASHBOX_DAY_AMMOUNT PRIMARY KEY (PK)
);
ALTER TABLE CASH_CASHBOX_DAY_AMMOUNT ADD CONSTRAINT FK_CASH_CASHBOX_DAY_AMMOUNT 
  FOREIGN KEY (CASHBOXID) REFERENCES CASH_CASHBOX
  (ID);

CREATE INDEX IDX_CCDA_CASHBOXID ON CASH_CASHBOX_DAY_AMMOUNT(CASHBOXID);
CREATE INDEX IDX_CCDA_DATE_TIME ON CASH_CASHBOX_DAY_AMMOUNT(DATE_TIME);

SET TERM ^^ ;
CREATE TRIGGER CASH_CASHBOX_DAY_AMMOUNT_PK ACTIVE BEFORE INSERT AS
BEGIN
  if (new.PK IS NULL) then
new.PK = GEN_ID(PK_GEN, 1);
END ^^

SET TERM ; ^^

If this makes the problem disappear for INSERTs, then that problem is you 
inserting identical CASHBOXID and DATE_TIME combinations.

If this is the problem, you'd probably want to change a few selects in your 
programs from (e.g.)

select cashboxid, date_time, ammount
from cash_cashbox_day_ammount

to

select cashboxid, date_time, sum(ammount) ammount
from cash_cashbox_day_ammount
group by 1, 2

since there now can be duplicates.

Another thing is that you now may want to do some occational housekeeping, 
allowing for duplicates to occur is not the same as wanting there to remain 
duplicates of CASHBOXID and DATE_TIME over time in your database. I'd recommend 
you to run this statement regularly, e.g. once each night or week (change it to 
a stored procedure if you want to - and if this table is huge, you may want to 
add another trigger-populated field with the insert and update time - so that 
you can exclude rows not changed/inserted recently from the calculation):

execute block as
declare variable cbid integer;
declare variable dt   numeric(18,0);
declare variable total_amount numberic(18,0);
begin
  for select cashboxid, date_time, sum(ammount)
  from cash_cashbox_day_ammount
  group by 1, 2
  having count(*)  1
  into :cbid, :dt, :total_amount do
  begin
delete from cash_cashbox_day_ammount
where cashbox_id = :cbid
  and date_time = :dt;
insert into cash_cashbox_day_ammount(cashboxid, date_time, ammount)
values(:cbid, :dt, :total_amount);
  end
end

HTH,
Set

[firebird-support] Re: Insert ot update statements locks a table and all other instances that try to run same statement are lcoked to

2014-09-05 Thread doyc...@dsoft-bg.com [firebird-support]
CASHBOXID is already know. It is a reference to a table that we don't touch in 
the transaction. 

This table contains the current total amount of a cash box at the begging of 
the day.

So when I try to load data for a cash box a java code checks is there record 
for this cash box for current day. If there is no such record it calculates the 
total current amount and calls the insert that adds a record to this table. 

Usually inserting record with same value for primary key will create error and 
probably will not  block the execution of the statement.

User is sysdba. There is no security restrictions for access to database.

End users have no access to database server at all. They all use code that runs 
in application server to access or modify data. That is why I'm not restricting 
the access to the database for the moment.

I'm not sure it is a lock conflict. But I suspect that something is locked 
somewhere and that forces the execution of the statement to stop and to lock 
all other inserts that other connections try to execute.

As I said I need ideas or hints what to do in order to find the real problem 
when this situation happens again.

One more thing. When I try to stop application server when this happens all 
connections that are blocked including the initial one stop the shutdown of the 
server. 

When I try to kill firebird process that initially blocked the execution all 
other processes are unlocked and server is shutdown after that. 

But in this case I try guess which process is that. When I look in the 
monitoring tables I can't see the pid of the process of each statement.