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

2014-09-06 Thread Mark Rotteveel m...@lawinegevaar.nl [firebird-support]
On 5-9-2014 19:48, doyc...@dsoft-bg.com [firebird-support] wrote:
 I'm not sure it is locked table.

 What I'm looking for is idea or advice how to find what the real problem is.

I'd start with obtaining a thread dump of the application when it is 
hanging. The stacktraces might hint at the cause. Also if you are using 
Firebird 2.5, use the trace facility for logging the activity.

Also consider upgrading to Jaybird 2.2.5; I fixed several concurrency 
bugs which could cause livelock or deadlock (although most of those were 
in parts you aren't using).

Other than that: investigate your code to make sure it isn't using long 
running transactions.

 My data source definition is this:

 connection-factories
tx-connection-factory
  jndi-namePowerProDS/jndi-name
  xa-transaction/
  track-connection-by-tx/
  rar-namejaybird-2.1.6.rar/rar-name
  connection-definitionjavax.sql.DataSource/connection-definition
  config-property name=Database
 type=java.lang.Stringlocalhost/3050:powerpro/config-property
  user-namesysdba/user-name
  passwordmasterkey/password
  config-property name=EncodingUNICODE_FSS/config-property
 ;  prepared-statement-cache-size100/prepared-statement-cache-size
  min-pool-size10/min-pool-size
  max-pool-size50/max-pool-size
metadata
   type-mappingEuroPro/type-mapping
/metadata
/tx-connection-factory
 /connection-factories


-- 
Mark Rotteveel


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

2014-09-06 Thread doyc...@dsoft-bg.com [firebird-support]
I'm using Firebird 2.1.6. Also I tried to upgrade to latest jaybird before but 
it does not work properly with JBoss 4.2.3. I remember something has changed 
and as soon as I try to start apps server and run client, exceptions were 
starting to popup in the log.

This code path uses long running transaction only if for some reason the 
history for a cash box is lost and has to be rebuild day by day. This is not 
the case in the moment.

usually the execution of this operation have to take not more then just a 
second or a little more when more then 5-6 cash boxes has to be recalculated.

[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