Working With Transfer ORM - Transfer Transaction Advice

Transfer , Database , ColdSpring Add comments
Maintaining referential integrity in your database is a good thing. Any decent RDBMS will include ACID (Atomicity, Consistency, Isolation, Durability) transaction support for ensuring consistency and recoverability of your database. With the Transfer ORM Transaction object, one can apply transactions easily to a Service or Gateway. Here's how one can use it to wrap all "save" and "delete" methods for a Service. The Transfer transaction object applies an aspect oriented programming (aop) advice to a method specified in the transaction advise() method. Which makes it dead simple to apply transactions with Transfer. <cfset transaction.advise(targetComponent,method)> You can also specify multiple methods using a regular expression. <cfset transaction.advise(targetComponent,regex)> The first step is to create singleton instance of the Transfer Transaction object in Coldspring and pass that as a constructor or setter argument to the Service. Here's a snippet from a sample Coldspring xml file showing the transfer, datasource, transaction, Service and Gateway bean definitions for a fictitious Image Gallery application with two objects. A Gallery that can have one to many Images. I've left out the transfer factory setup for brevity. <bean id="transfer" factory-bean="transferFactory" factory-method="getTransfer" />
<bean id="datasource" factory-bean="transferFactory" factory-method="getDatasource" />
<bean id="transaction" factory-bean="transferFactory" factory-method="getTransaction" />

<bean id="GalleryGateway" class="model.gallery.GalleryGateway">
<constructor-arg name="transfer">
<ref bean="transfer" />
</constructor-arg>
<constructor-arg name="datasource">
<ref bean="datasource" />
</constructor-arg>
</bean>

<bean id="ImageGateway" class="model.gallery.ImageGateway">
<constructor-arg name="transfer">
<ref bean="transfer" />
</constructor-arg>
<constructor-arg name="datasource">
<ref bean="datasource" />
</constructor-arg>
</bean>

<bean id="GalleryService" class="model.gallery.GalleryService">
<constructor-arg name="transaction">
<ref bean="transaction" />
</constructor-arg>
<property name="GalleryGateway">
<ref bean="GalleryGateway" />
</property>
<property name="ImageGateway">
<ref bean="ImageGateway" />
</property>
</bean>
In the Service init method, the transaction advice is applied with one line of code. <cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="transaction" type="transfer.com.sql.transaction.Transaction" required="true" />
<cfset arguments.transaction.advise(this,"^(save|delete)")>
<cfreturn this>
</cffunction>
To delete a Gallery and all of its Images, I might have the following method in my Service. <cffunction name="deleteGallery" access="public" returntype="void" output="false">
<cfargument name="GalleryId" type="numeric" required="true">
<cfset var Gallery = getGalleryGateway().get(arguments.GalleryId)
<cfset var images = Gallery.getImageArray()>

<cfset var i = 1>
<cfloop from="1" to="#ArrayLen(images)#" index="i">
<cfset getImageGateway().delete(images[i])>
</cfloop>
<cfset getGalleryGateway().delete(Gallery)>
</cffunction>
Having applied the transaction advice to all save and delete methods in my Service ensures that the database will be left intact should anything fail. For an in depth explanation of working with transactions in transfer, see the wiki documentation.

1 response to “Working With Transfer ORM - Transfer Transaction Advice”

  1. UGG Says:
    <p><a href="http://www.uggbootsretails.com"; target="_blank">UGG Argyle Knit</a> features genuine twin-face sheepskin for refreshing comfort. It's one of our tallest boots and looks great all the way up or cuffed down. <a href="http://www.uggbootsretails.com"; target="_blank">UGG boots</a> in our Classic Collection feature a soft foam insole covered with genuine sheepskin and have a molded EVA light such as <a href="http://www.uggbootsretails.com"; target="_blank">UGG Sundance</a> and <a href="http://www.uggbootsretails.com"; target="_blank">UGG Bailey Button</a>, the flexible outsole designed for amazing comfort with every step. Beat the chill in superior style with the <a href="http://www.uggbootsretails.com"; target="_blank">UGG Ultra Tall</a>. The <a href="http://www.uggbootsretails.com"; target="_blank">Bailey Button 1873</a> of light and flexible outsole along with a suede heel guard provides durable wear all season long. Detailed with three oversized wood buttons, this style can be styled up and buttoned, slouched and slightly unbuttoned, or completely cuffed. <a href="http://www.uggbootsretails.com"; target="_blank">UGG Ultra Short</a> is also a good choice for people to refuge from the cold.Just buy one of the ugg boots for your family and yourself, it’s really a good gift both for you famlily and you.Action is better than thinking in heart.</p>

Leave a Reply



Powered by Mango Blog. Design and Icons by N.Design Studio