Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   Transacciones en Firebird (https://www.clubdelphi.com/foros/showthread.php?t=58583)

jsanchez 24-07-2008 18:21:52

Transacciones en Firebird
 
Hola,
Utilizo la base de datos Firebird 2.1.

Tengo un modulo que accede a unas tablas para hacer unas inserciones.
Se hace a traves de unas transacciones.
Código Delphi [-]
try
   basedatos.starttransaction;
   // insert en varias tablas, y bastantes calculos y consultas. 

   basedatos.commit;
except
   ShowMessage('error');
   basedatos.rollback;
end;

Al utilizar el sistema en red, cada cierto tiempo daba error el proceso, pero si inmediatamente se volvía a generar ese proceso anterior, lo hacía sin problemas.

Así que se intentó "catch" el problema, o sea la excepción, y daba un error del tipo
Código:

lock conflict in wait transaction
violation of primary key or unique key constraint .....

Parece ser que como se accede a una tabla para hacerle unos insert que se utiliza desde varias partes del programa, otro usuario en ese momento también estaba accediendo a la tabla y por eso da error.

Entonces mi pregunta es si no hay algo parecido al
Código SQL [-]
select for update
de MySQL, pero para hacerlo con Firebird.

Muchas Gracias.

RolphyReyes 24-07-2008 18:40:16

Saludos.

Como consejo revisa bien el manejo de las transacciones por lo regular no es buena practica el realizar bloqueos explícitos.

Te pongo la referencia del Release Notes de FB 1.5:
Cita:

(1.5) Explicit locking
Nickolay Samofatov
The addition of the optional WITH LOCK clause provides a limited explicit pessimistic locking capability
for cautious use in conditions where the affected row set is
1. extremely small (ideally, a singleton) AND
2. precisely controlled by the application code.
Caution
This is for experts only!
The need for a pessimistic lock in Firebird is very rare indeed and should be well understood before
use of this extension is considered.
It is essential to understand the effects of transaction isolation and other transaction attributes before
attempting to implement explicit locking in your application.
Syntax Pattern
SELECT ... FROM <sometable>
[WHERE ...]
[FOR UPDATE [OF ...]]
[WITH LOCK]
...;
If the WITH LOCK clause succeeds, it will secure a lock on the selected rows and prevent any other
transaction from obtaining write access to any of those rows, or their dependants, until your transaction
ends.
If the FOR UPDATE clause is included, the lock will be applied to each row, one by one, as it is
fetched into the server-side row cache. It becomes possible, then, that a lock which appeared to succeed when requested will nevertheless fail subsequently, when an attempt is made to fetch a row
which becomes locked by another transaction.
The SELECT... WITH LOCK construct is available in DSQL and PSQL.
Important
The SELECT... WITH LOCK construct can succeed only in a top-level, single-table SELECT statement.
It is not available
• in a subquery specification
• for joined sets
• with the DISTINCT operator, a GROUP BY clause or any other aggregating operation
• with a view
• with the output of a selectable stored procedure
• with an external table

Examples using Explicit Locking
i) (simple)
Código SQL [-]
SELECT * FROM DOCUMENT WHERE ID=? WITH LOCK

ii) (multiple rows, one-by-one processing with DSQL cursor)
Código SQL [-]
SELECT * FROM DOCUMENT WHERE PARENT_ID=?
FOR UPDATE WITH LOCK

Hasta luego.

Ferrari 25-07-2008 04:05:12

Creo ke te dejaron igual...jajajaja

josejairoms 04-02-2015 17:15:06

transacciones..
 
A alguien le puede servir... al fin y al cabo a eso vinimos al club a aportar algo..

Tal como lo comenta RolphyReyes.. el uso de las transacciones no puede ser tan general, mas ahun si se están ejecutando sentencias sobre varias tablas.. lo recomendable es buscar sobre temas bloqueos pesimistas y/o bloqueos optimistas.. me parece como plantea inicialmente el problema funcionara muy bien en un programa que se ejecute en una sola maquina, en un ámbito de red tendrá muchos problemas..

Yo estoy teniendo situaciones donde requiero hacer un manejo adecuado de transacciones y espero continuar alimentando el hilo hasta que encuentre solución a mi situación y con eso ayudamos a otros..

Casimiro Noteví 04-02-2015 17:50:01

Cita:

Empezado por josejairoms (Mensaje 488432)
..

http://www.intitec.com/varios/transacciones-0.2.6.pdf

josejairoms 04-02-2015 19:58:42

notevi..
 
Gracias muy gentil, esta muy completico el documento....

Si te vi y te vi muy bien...

gracias...

Casimiro Noteví 04-02-2015 20:04:13

Cita:

Empezado por josejairoms (Mensaje 488437)
Gracias muy gentil, esta muy completico el documento....

Si te vi y te vi muy bien...

gracias...

^\||/^\||/^\||/


La franja horaria es GMT +2. Ahora son las 20:31:30.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi