Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MTS transactions

Status
Not open for further replies.

grad

Programmer
Apr 13, 2000
15
0
0
US
I am new to MTS but I have developed COM objects in VB before.&nbsp;&nbsp;I am using VB6.<br><br>Scenario:<br>If I have a functions withdraw(account, amount) and <br>deposit(account, amount)<br>the two functions can process transactions independently.&nbsp;&nbsp;Within each function I connect, do work and then disconnect from the database.<br><br>Question:<br>can I do the following in the class/com object<br><br>function transfer(account1, account2, amount)<br>&nbsp;&nbsp;&nbsp;withdraw(account1, amount)<br>&nbsp;&nbsp;&nbsp;deposit(account2, amount)<br>&nbsp;&nbsp;&nbsp;setcomplete/setabort<br><br>and MTS will know that both transactions need to be committed or aborted as a unit<br><br>thanks<br>mark<br><br><br><br><br><br>
 
hi grad,<br><br>The answer to your question is YES. MTS will take it as one unit.<br><br>Backgrounders:-<br><br>To be part fo a transaction a component running under MTS must be configured under one of the following categories.<br><br>1. Requires new Transaction<br>2. Requires Transaction<br>3. Supports Transaction.<br><br>For case 1 whenever the component is instantiated a new transaction is started for it automatically by MTS.<br><br>For case 2 whenever the component is instantiated there are 2 possibilities.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;a. If the component A is instantiated from inside a method of another component B that is already in a transaction then component A becomes part of the transaction of component B.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b. If the above is not the case then a new transaction is automatically started by MTS.<br><br>For case 3 if the component is instantiated from inside the method of another component that is already in a transaction then it becomes part of the transaction else no transaction is started for it. This is similar to case 2 with a change in option b.<br><br><br>The component developer need not explicitly start a transaction. He only needs to configure it under MTS. What he needs to do however is to either commit or abort the transaction. Therefore all database operations become part of a transaction until setabort or setcomplete is called.<br>Taking your case when the function transfer is called and provided the component is configured be transactable then until a setabort/setcomplete is called all database operations will be taken as one unit even though you may have opened and closed the connection many times in between.<br><br>note:- it is always good practise to call setabort/setcomplete at the end of every method because apart from transaction these statements tell MTS that I have done with the object and you can deactivate it. this improves scalability.<br><br><br>Does that clear your doubts????
 
This helps a lot now for my second question!!<br><br>On my client when I create an instance of the object, MTS starts a surrogate process.&nbsp;&nbsp;Can I get the object context in my client and do the commits and aborts from the client or do I have to create another COM object under MTS to do this.<br><br>i.e.<br><br>This code will reside on the client...<br><br>sub clientcode()<br>&nbsp;&nbsp;&nbsp;dim o1 as com.class<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;set o1 = createobject(&quot;com.class&quot;) 'creates the object&nbsp;&nbsp;&nbsp;within MTS<br><br>&nbsp;&nbsp;&nbsp;&nbsp;i = o1.f1()<br>&nbsp;&nbsp;&nbsp;&nbsp;j = o1.f2()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if i = -1 or j = -1 then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getobjectconext().setabort<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getobjectcontext().setcomplete<br>&nbsp;&nbsp;&nbsp;end if<br><br><br><br><br><br><br><br>
 
hi grad ,<br><br>first of all you cannot call &quot;getobjectcontext&quot; directly in your code even though this might not actually result in an error. <br><br>in your VB code you should first implement Object control.<br><br>eg:-<br><br>implements ObjectControl <br><br><br>this will have 3 events called:-<br>1.Objectcontrol_activate()<br>2.Objectcontrol_deactivate()<br>3. objectcontrol_canbepooled()<br><br>inside objectcontrol_activate() you should write<br>set ctx=getobjectcontext()<br><br>the activate event will be fired automatically when the first method of the object is called which will initialize<br>the object context.<br><br>so as per your code:-<br><br>if i = -1 or j = -1 then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getobjectconext().setabort&nbsp;&nbsp;----&gt;&gt;&gt; error <br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getobjectcontext().setcomplete&nbsp;&nbsp;---&gt;&gt;&gt; error<br>&nbsp;&nbsp;&nbsp;end if<br><br><br>the most likely error would be:-<br><br>&quot;with or set variable not set&quot;.<br><br><br><br>So the answer to your question would be NO. You cannot do as you had suggested. Believe me I just tried it out.<br><br>Please correct me if I am wrong<br><br>bye
 
Hi<br><br>Lovely julia has explained clearly about proper way of writing an MTS Component using an ObjectContext.&nbsp;&nbsp;But, I feel she hasnt answered what grad asked, from what i understand...<br><br>&quot;Can I get the object context in my client and do the commits and aborts from the client or do I have to create another COM object under MTS to do this&quot;<br><br>To my knowledge, you cannot get the object context in the client.&nbsp;&nbsp;You have to create another COM object.&nbsp;&nbsp;If you are going to control transactions in the Client... the whole idea of MTS fails.<br><br>Hope I answered you.<br><br>Bye<br>Jacky
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top