You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Legolas Woodland <le...@gmail.com> on 2006/04/13 00:59:48 UTC

how to apply transactional insert is a scenario that 3 tables are going to change?


Hi thank you for reading my post
How i can apply transaction in a scenario like this :

In one of my web applicatinon page , i have 3 functions that each of 
them update/insert data to a table
I use connection pool to retrieve a connection in each of those 
functions (i can use one connection for all of functions but i do not 
know whether it is  good or not).
Now the changes to database just should be applied if all of those 
functions insert data sucessfully.
my problem is that i do not know how i can use transaction in this case.

I need just the transaction over these inserts , which kind of commit 
mode i should use?

Re: how to apply transactional insert is a scenario that 3 tables are going to change?

Posted by Legolas Woodland <le...@gmail.com>.
Oystein Grovlen - Sun Norway wrote:
> Legolas Woodland wrote:
>>
>>
>> Hi thank you for reading my post
>> How i can apply transaction in a scenario like this :
>>
>> In one of my web applicatinon page , i have 3 functions that each of 
>> them update/insert data to a table
>> I use connection pool to retrieve a connection in each of those 
>> functions (i can use one connection for all of functions but i do not 
>> know whether it is  good or not).
>> Now the changes to database just should be applied if all of those 
>> functions insert data sucessfully.
>> my problem is that i do not know how i can use transaction in this case.
>
> If you want to run all functions in the same transaction, they need to 
> use the same connection.
>
>>
>> I need just the transaction over these inserts , which kind of commit 
>> mode i should use?
>
> How about something like this:
>
> Connection conn = getConnectionFromPool();
> conn.setAutoCommit(false);
> boolean s1 = f1(conn);
> boolean s2 = f2(conn);
> boolean s3 = f3(conn);
> if (s1 && s2 && s3) {
>     conn.commit();
> } else {
>     conn.rollback();
> }
>
Thank you for reply , Shall we call rollback , or driver will not commit 
the changes until we call commit ?
How i should determine values of those boolean ?


Thanks

Re: how to apply transactional insert is a scenario that 3 tables are going to change?

Posted by Oystein Grovlen - Sun Norway <Oy...@Sun.COM>.
Legolas Woodland wrote:
> 
> 
> Hi thank you for reading my post
> How i can apply transaction in a scenario like this :
> 
> In one of my web applicatinon page , i have 3 functions that each of 
> them update/insert data to a table
> I use connection pool to retrieve a connection in each of those 
> functions (i can use one connection for all of functions but i do not 
> know whether it is  good or not).
> Now the changes to database just should be applied if all of those 
> functions insert data sucessfully.
> my problem is that i do not know how i can use transaction in this case.

If you want to run all functions in the same transaction, they need to 
use the same connection.

> 
> I need just the transaction over these inserts , which kind of commit 
> mode i should use?

How about something like this:

Connection conn = getConnectionFromPool();
conn.setAutoCommit(false);
boolean s1 = f1(conn);
boolean s2 = f2(conn);
boolean s3 = f3(conn);
if (s1 && s2 && s3) {
     conn.commit();
} else {
     conn.rollback();
}

-- 
Øystein