Board index » jbuilder » how to save several dataset in a transaction

how to save several dataset in a transaction


2006-05-19 02:17:45 PM
jbuilder22
suppose there is two dataset (one header and one detail), I want to save these two datasets in a transaction so that if any dataset can apply the update to the SQL server, then rollback all changes in both dataset, how can I do this (is there any code sample)?
I have seen there is savechange(dataset[], boolean,boolean,boolean), is this method help to save all dataset in one transaction and can commit or rollback all?
thx!!
 
 

Re:how to save several dataset in a transaction

I try to use the follow method:
Database.setAutoCommit(false);
try{
// some change made on ds;
database.saveChanges(ds);
throw new Exception("test rollback()");
database..commit();
}
catch(Exception ex){
Database.rollback();
}
finally{
Database.setAutoCommit(true);
}
but I find that even I set autocommit to false, when database.savechanges(ds) is called, the change is already written to database (I check using SQL analyzer), and even there is exception and rollback() is run, the record has not really rollback(), so the change is still written to the database table, why??
Thx!!
"kei" < XXXX@XXXXX.COM >wrote:
Quote

suppose there is two dataset (one header and one detail), I want to save these two datasets in a transaction so that if any dataset can apply the update to the SQL server, then rollback all changes in both dataset, how can I do this (is there any code sample)?
I have seen there is savechange(dataset[], boolean,boolean,boolean), is this method help to save all dataset in one transaction and can commit or rollback all?
thx!!
 

Re:how to save several dataset in a transaction

I have try to change the savechanges method as follows:
database.saveChanges(ds, false); // use false parameter
now the database can rollback, but an error " DataSet has no unique row identifiers. " is prompted, why?
"kei" < XXXX@XXXXX.COM >wrote:
Quote

I try to use the follow method:

Database.setAutoCommit(false);
try{
// some change made on ds;
database.saveChanges(ds);
throw new Exception("test rollback()");
database..commit();
}
catch(Exception ex){
Database.rollback();
}
finally{
Database.setAutoCommit(true);
}

but I find that even I set autocommit to false, when database.savechanges(ds) is called, the change is already written to database (I check using SQL analyzer), and even there is exception and rollback() is run, the record has not really rollback(), so the change is still written to the database table, why??

Thx!!

"kei" < XXXX@XXXXX.COM >wrote:
>
>suppose there is two dataset (one header and one detail), I want to save these two datasets in a transaction so that if any dataset can apply the update to the SQL server, then rollback all changes in both dataset, how can I do this (is there any code sample)?
>I have seen there is savechange(dataset[], boolean,boolean,boolean), is this method help to save all dataset in one transaction and can commit or rollback all?
>thx!!

 

{smallsort}

Re:how to save several dataset in a transaction

anyone willing to help?
"kei" < XXXX@XXXXX.COM >wrote in message
Quote

I have try to change the savechanges method as follows:
database.saveChanges(ds, false); // use false parameter

now the database can rollback, but an error " DataSet has no unique row
identifiers. " is prompted, why?

"kei" < XXXX@XXXXX.COM >wrote:
>
>I try to use the follow method:
>
>Database.setAutoCommit(false);
>try{
>// some change made on ds;
>database.saveChanges(ds);
>throw new Exception("test rollback()");
>database..commit();
>}
>catch(Exception ex){
>Database.rollback();
>}
>finally{
>Database.setAutoCommit(true);
>}
>
>but I find that even I set autocommit to false, when
>database.savechanges(ds) is called, the change is already written to
>database (I check using SQL analyzer), and even there is exception and
>rollback() is run, the record has not really rollback(), so the change is
>still written to the database table, why??
>
>Thx!!
>
>"kei" < XXXX@XXXXX.COM >wrote:
>>
>>suppose there is two dataset (one header and one detail), I want to save
>>these two datasets in a transaction so that if any dataset can apply the
>>update to the SQL server, then rollback all changes in both dataset, how
>>can I do this (is there any code sample)?
>>I have seen there is savechange(dataset[], boolean,boolean,boolean), is
>>this method help to save all dataset in one transaction and can commit or
>>rollback all?
>>thx!!
>

 

Re:how to save several dataset in a transaction

kei wrote:
Quote
I have try to change the savechanges method as follows:
database.saveChanges(ds, false); // use false parameter

now the database can rollback, but an error " DataSet has no unique row identifiers. " is prompted, why?

Not seeing your SQL it is hard to provide a specific answer.. Generally
this is caused by having a JOIN in the SQL such that DataExpress can not
reliably determine which fields are the "key" fields.. (Since the meta
data for a JOIN does not return this info.. I'm assuming..)
Following is an example of the various things you might need to do to
solve this..
// A query with a join..
StringBuffer _sb = new StringBuffer();
_sb.append("SELECT DISTINCT
S.INVOICE_NO,S.PLANT_ID,S.SEQ,S.DISCOUNT_CODE,");
_sb.append("S.DATE_SOLD,S.QUANTITY_SOLD,S.BASE_PRICE,S.EXTENSION,");
_sb.append("S.POSTED,S.LOADUNIT,S.EXTLOAD,S.CHOICECODE,S.LINEDISC,");
_sb.append("S.SKU,MIV.CATEgory_NO,MIV.COMMON_NAME, MIV.DISPLAY_SORT ");
_sb.append("FROM SALES S JOIN MASTERINV MIV ON S.PLANT_ID =
MIV.PLANT_ID ");
_sb.append("WHERE S.INVOICE_NO = "+(new
Double(arg_dInvNo)).intValue());
_sb.append(" ORDER BY MIV.DISPLAY_SORT");
//Using the above query as an example the following MIGHT be required.
// Your milage may vary..
QueryDataSet _qds = (QueryDataSet)this.getData(_sb.toString());
// set the table name that you intend to update
_qds.setTableName("SALES");
// set the row IDs
_qds.setRowId("INVOICE_NO", true);
_qds.setRowId("PLANT_ID", true);
_qds.setRowId("SEQ", true);
// set which fields came from the join and should be ignored on an update
_qds.getColumn("COMMON_NAME").setResolvable(false);
_qds.getColumn("CATEgory_NO").setResolvable(false);
_qds.getColumn("DISPLAY_SORT").setResolvable(false);
// optional sort..
_qds.setSort(new SortDescriptor(new String[] {"DISPLAY_SORT"}
, true, false));
--
=============================================
TeamB are volunteer helpers. Please DO NOT REPLY VIA EMAIL!
Post all questions and replies to this newsgroup ONLY
For papers on DataExpress, Applets, JSP, and Web Development go to:
www.microps.com/mps/paperFAQ.html
====================================================
 

Re:how to save several dataset in a transaction

kei wrote:
Quote

but I find that even I set autocommit to false, when database.savechanges(ds) is called, the change is already written to database (I check using SQL analyzer), and even there is exception and rollback() is run, the record has not really rollback(), so the change is still written to the database table, why??

Thx!!
try..
public final void saveChanges(DataSet[] dataSets,
boolean doTransactions,
boolean postEdits,
boolean resetPendingStatus)
Parameters:
dataSets - Array of dataSets to save changes for.
doTransactions - true: All changes will be in 1 transaction. (default)
false: No transactions calls will be made.
postEdits - true: All edits are posted before changes are saved.
(default) false: No edits are saved.
--
=============================================
TeamB are volunteer helpers. Please DO NOT REPLY VIA EMAIL!
Post all questions and replies to this newsgroup ONLY
For papers on DataExpress, Applets, JSP, and Web Development go to:
www.microps.com/mps/paperFAQ.html
====================================================
 

Re:how to save several dataset in a transaction

"John Moore (TeamB)" < XXXX@XXXXX.COM >wrote:
Quote
kei wrote:

>
>but I find that even I set autocommit to false, when database.savechanges(ds) is called, the change is already written to database (I check using SQL analyzer), and even there is exception and rollback() is run, the record has not really rollback(), so the change is still written to the database table, why??
>
>Thx!!

try..

public final void saveChanges(DataSet[] dataSets,
boolean doTransactions,
boolean postEdits,
boolean resetPendingStatus)

Parameters:
dataSets - Array of dataSets to save changes for.
doTransactions - true: All changes will be in 1 transaction. (default)
false: No transactions calls will be made.
postEdits - true: All edits are posted before changes are saved.
(default) false: No edits are saved.

yes, now I use saveChanges(DataSet[] dataSets,
boolean doTransactions) and set the doTransactions to false, then the transaction can rollback(), just feel strange why I need to set the doTransactions paramters to false!! (from the help manual, false means No transactions calls will be made, but for using transaction,it seems that the paramter must be set to false!!)
 

Re:how to save several dataset in a transaction

"John Moore (TeamB)" < XXXX@XXXXX.COM >wrote:
Quote
Not seeing your SQL it is hard to provide a specific answer.. Generally
this is caused by having a JOIN in the SQL such that DataExpress can not
reliably determine which fields are the "key" fields.. (Since the meta
data for a JOIN does not return this info.. I'm assuming..)
my SQL is only retrieve one table, no join, anyway,I can fix it now by
dsDetail.setMetaDataUpdate(MetaDataUpdate.NONE);
and then set a column rowID to true.
Thx!!
 

Re:how to save several dataset in a transaction

kei wrote:
Quote
"John Moore (TeamB)" < XXXX@XXXXX.COM >wrote:
yes, now I use saveChanges(DataSet[] dataSets,
boolean doTransactions) and set the doTransactions to false, then the transaction can rollback(), just feel strange why I need to set the doTransactions paramters to false!! (from the help manual, false means No transactions calls will be made, but for using transaction,it seems that the paramter must be set to false!!)
What it means is that that call to "saveChanges" WILL NOT commit the
transaction. You have told it you will handle it MANUALLY..
John..
--
=============================================
TeamB are volunteer helpers. Please DO NOT REPLY VIA EMAIL!
Post all questions and replies to this newsgroup ONLY
For papers on DataExpress, Applets, JSP, and Web Development go to:
www.microps.com/mps/paperFAQ.html
====================================================