![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I want to be able to work with 2 or more TableAdapters within one transaction. I understand that I need to extend the appropriate TableAdapter partial classes. This isn't too much of a problem because I only have 10 or so database tables and TableAdapters to worry about. |
#3
| |||
| |||
|
|
You could also write something like a factory that ties all TableAdapters in a transaction. |
#4
| |||
| |||
|
|
Hi Sahil I've poked around a bit more to see what the TableAdapter is doing and managed to get the commands enlisted in a transaction for one TableAdapter. Now I just need to work out how to add another one. I took delivery of your ADO.NET book yesterday so I hope to advance my understanding of this wondrous technology soon. You could also write something like a factory that ties all TableAdapters in a transaction. But this I fear may well be beyond me for a while yet, although I do like the idea. Thanks Andrew |
#5
| |||
| |||
|
|
I hope you like the book - even though I've advocated against TableAdapters in Chapter 3 for the very reasons you are running into. |
|
Anyway, the idea of factory would be something as simple as creating an EnlistInTransaction method on each TableAdapter. You could then have each TableAdapter implement a specific custom interface, and then you could create a class on top that calls EnlistInTransaction for every instance of that interface passed in. |
#6
| ||||
| ||||
|
|
OK. I thought I could loop through a dictionary collection of say 'ITransTableAdapter' interfaces. The business layer can add all the TAs in and then call them by the key once they have been enlisted in the transaction. |
|
Can I start a transaction on the first adapter in the loop and then pass it to each subsequent adapter in the collection? |
|
Can you give me an idea of how the EnlistInTransaction method might look? |
|
Hi I hope you like the book - even though I've advocated against TableAdapters in Chapter 3 for the very reasons you are running into.Yes, the lack of support for transactions has caught me out a bit. Anyway, the idea of factory would be something as simple as creating an EnlistInTransaction method on each TableAdapter. You could then have each TableAdapter implement a specific custom interface, and then you could create a class on top that calls EnlistInTransaction for every instance of that interface passed in. OK. I thought I could loop through a dictionary collection of say 'ITransTableAdapter' interfaces. The business layer can add all the TAs in and then call them by the key once they have been enlisted in the transaction. Can I start a transaction on the first adapter in the loop and then pass it to each subsequent adapter in the collection? Can you give me an idea of how the EnlistInTransaction method might look? Does this sound reasonable? It's a bit messy using keys but I can't think how else to do it. I think it's starting to look like a workable plan. Your comments will be very much appreciated. Thanks again Andrew |
#7
| |||
| |||
|
#8
| |||
| |||
|
#9
| |||
| |||
|
|
J055, You have a great solution here! I had something similar, but less elegant, as it exposed SqlClient classes to the BLL. The problem I was having with my solution is that I didn't know how to enlist the Select commands (they were always null after I created the TableAdapter. As a result, I had to do all my d/b reads first, storing data in Lists and Hashtables, then open the transaction and issue updates, deletes, etc. Your solution taught me about the _commandCollection, which solved that problem. I like the complete encapsulation within the DAL, as well. Thanks a lot for your example code! Ray "J055" wrote: Oops! I said I hadn't done much testing. You need to add a rollback method to the AdapterTransactor class. public void Rollback() { // Rollback if it all goes wrong trans.Rollback(); if (conn.State == System.Data.ConnectionState.Open) conn.Close(); } Then put your adapter transactions is a try catch block AdapterTransactor atx = new AdapterTransactor(); atx.AddAdapter(adapter1); atx.AddAdapter(adapter2); atx.AddAdapter(adapter3); atx.BeginTransaction(); try { // transactional work on adapters atx.Commit(); } catch (Exception) { atx.Rollback(); throw; } Otherwise Rollback will never be reached if there's an exception. Thanks Andrew |
#10
| |||
| |||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |