CodeSmith Community
Your Code. Your Way. Faster!

Use of TransactionManager

Latest post 06-18-2007 4:51 PM by hydtohouston. 8 replies.
  • 09-29-2005 10:50 AM

    Use of TransactionManager

    Hello
     
    I have the following code which add a company then an employee:
     
    try
    {
    transactionManager = SqlDataRepository.CreateTransaction();
    transactionManager.BeginTransaction();    
    DataRepository.CompanyProvider.Insert( company );
    DataRepository.EmployeeProvider.Insert( employee );
    transactionManager.Commit();
    }
    catch( Exception e )
    {
     if ( ( transactionManager != null )
     && ( transactionManager.Isopen ) )
     {
     transactionManager.Rollback();
     }
    }
     
    what I would like is that the company insert would be rolled back if the employee insert fails.
     
    But I'm not able to do that.
    I get the following trace in sql profiler when there is an error during the insert of the employee :
     
    RPC:Completed exec sp_reset_connection
    SQL:BatchCompleted SET TRANSACTION ISOLATION LEVEL READ COMMITTED;BEGIN TRANSACTION RPC:Completed exec sp_reset_connection
    RPC:Completed exec sp_executesql N' INSERT INTO dbo.[Company]()VALUES()
    RPC:Completed exec sp_reset_connection
    RPC:Completed exec sp_executesql N' INSERT INTO dbo.[Employee]()VALUES ()
    SQL:BatchCompleted IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
     
    Could you explain me what is wrong in my code please ?
     
    Regards
     
    Chris
    • Post Points: 65
  • 09-29-2005 11:52 AM In reply to

    RE: Use of TransactionManager

    Hello

    I get the last templates from CVS and now I get the following error when i build my project :

    error CS0619: '...SqlDataRepository' is obsolete: 'This class will not exist in future version of nettiers. Use the DataRepository class instead.'

    But i don't find a way to create a transaction without SqlDataRepository.

    Could you help me please ?

    Regards

    Chris
    • Post Points: 5
  • 09-29-2005 1:58 PM In reply to

    RE: Use of TransactionManager

    You should now be able to create the trans directly from the DataRepository now instead of having to go from the SqlDataRepository. Each provider should now show whether or not they support transactions, and the DataRepository will create one for you if available, for example, the WebServices provider would not have the ability to create transactions.

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
  • 09-30-2005 4:34 AM In reply to

    RE: Use of TransactionManager

    Hello

    Thanks for your feedback.

    ok...

    a sample code would be welcome :)

    And if the transaction is in the provider, how can i get a transaction which is used through two different DataProvider ?

    Regards

    Chris
    • Post Points: 5
  • 09-30-2005 9:48 AM In reply to

    • dSquared
    • Top 10 Contributor
    • Joined on 09-01-2004
    • Paris - France
    • Posts 519
    • Points 260,935

    RE: Use of TransactionManager

    your code is almost perfect, but you forgot to pass the transactionManager instance to the DALC methods.
    here is the working one (target the latest CVS, with former one, replace DataRepository by SqlDataRepository):
     
    try
    {
    transactionManager = DataRepository.CreateTransaction();
    transactionManager.BeginTransaction();
    DataRepository.CompanyProvider.Insert(transactionManager, company );
    DataRepository.EmployeeProvider.Insert(transactionManager, employee );
    transactionManager.Commit();
    }
    catch( Exception e )
    {
    if ( ( transactionManager != null ) && ( transactionManager.Isopen ) )
    {
    transactionManager.Rollback();
    }
    }
    changes are in bold.
     
    hope it helps

    John Roland
    ------------------------------
     Member of the .netTiers team
      http://en.serialcoder.net
    ------------------------------

    • Post Points: 70
  • 06-13-2007 3:11 PM In reply to

    Re: RE: Use of TransactionManager

    I am a beginner and am using Nettiers for the first time. 

    I have used the above code in my program and it has a similar requirement that if the employee insertion fails then the company details also should not be inserted. I have a few questions to ask you:

    1) Why are you checking for (transactionManager != null)  and (transactionManager.Isopen) in the catch block? Can you explain why would transactionManager be 'null' if the records where inserted? Can you also explain the other condition too.

     
    2) When will the data be actually inserted into the database? When the transactionManager.Commit() is executed? If yes, then what happens when the two Insert statements above the Commit are executed?

    Thanks,

    hydtohouston

     

    Filed under:
    • Post Points: 35
  • 06-18-2007 2:39 PM In reply to

    Re: RE: Use of TransactionManager

    Does nobody have answers to my questions? Please guys your answers will help me a lot in my project.

    Regards,

    hydtohouston. 

    • Post Points: 5
  • 06-18-2007 4:07 PM In reply to

    • marioh
    • Top 50 Contributor
    • Joined on 02-21-2006
    • Boston, MA
    • Posts 90
    • Points 2,580

    Re: RE: Use of TransactionManager

    1) It's always safe to check whether your transaction manager was initialized and whether your transaction is still open before trying to roll it back. Examples are an exception being thrown before opening the transaction and sql server deciding to rollback the transaction based on some sql error.

    2) This has nothing to do with .net tiers and is a sql/ado.net question.

    When you perform the 2 insert statements above and before you commit (assuming an isolation level that claims an exclusive lock on the rows), the rows are written to your database but are logically non-existent until they get commited. If you're new to sql server, you probably want to ready about transaction logs, isolation levels, locks and how sql server persists and organizes data.

    Even "writing" to the database is not as straight forward as you would imagine. sql server databases have one or multiple database files (.mdf) and one or multiple transaction log files (.ldf). Whenever you perform any update/insert/delete, sql server doesn't directly update your database file, rather sequentially writes entries to the transaction log. each entry in the log is labeled with a log sequence number (or LSN) and has enough information to be rolled forward (redo) or rolled back (undo). Entries in the log file that were written under the same transaction and linked together to make it easy to undo them or redo them together (undo in the case of a rollback). sql server then manages updating your data pages from the log entries. whenever you commit a transaction, sql server will make sure it writes the log entries to disk before it sends back a commit acknowledgement on commits.

    • Post Points: 60
  • 06-18-2007 4:51 PM In reply to

    Re: RE: Use of TransactionManager

     Thanks a lot for your reply. This really cleared all of my doubts. I really appreciate it.

     Regards,

    hydtohouston.
     

    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems