`
dellsoft
  • 浏览: 110850 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

grails transaction

阅读更多

http://sacharya.com/transactions-in-grails/

Transaction & Batch-processing in Grails

February 1, 2009

Transaction handling is one of the more complex areas of web development. Anytime a user takes any action in the interface which demands a couple of database actions in the backend, then usually you end up having do it as transaction. For a user, everything is either a success or failure. Partial success may be either harmful to the system or doesn’t mean anything to the user. Grails, since it is built on top of Spring and Hibernate, uses their underlying mechanism to deal with transactions. While it may seem confusing in the beginning, Grails actually makes it even more easier.

Lets see it in code.

Suppose we are designing an Offers System, like the one that your bank sends notifying you of some offers and promotions on services. A simplified object structure may be like the following:

1.class Offer {
2.String title
3.String content
4.Date expirationDate
5.Date createdDate = new Date()
6. 
7.static hasMany = [recipients: Recipient]
8.}
1.class Recipient {
2.String email
3. 
4.static belongsTo = [offer: Offer]
5.}

The relationship is fairly simple. Each Offer can have many Recipients, each Recipient belongs to an Offer.

belongsTo just means the offer Recipient will be deleted whenever the Offer is deleted, which makes sense coz we don’t want to keep the junk if the Offer itself is deleted.

hasMany lets you access to the Recipients from the Offer object. In other words, if you have the Offer object, you can dooffer.recipients and get the list of all the recipients for the Offer. Cool.

Now, here is how it will work. We want to add an Offer and some Recipients. Everything should either succeed or fail. Even if only one Recipient out of thousands fail, everything should fail.

There are two ways to do transactions in Grails:

1. Method-level Transaction: By default, each Service class in grails has a transactional property set to true. So if any public method in a Service class throws a RuntimeException or any Error, the transaction will be rolled back. If you do not need this default transaction property, you can explicitly set transactional to false.

In the code below, the OfferService throws a Runtime Exception anytime it isn’t able to add the Fffer or the Recipient, and the transaction will be rolled back.
You wouldn’t get the same transactional behavior if the method throws any Checked Exception or if the transactionalproperty is set to false.

01.class OfferService {
02. 
03.boolean transactional = true
04. 
05.def save(offer, recipients) {
06.if(!offer.validate()) {
07.throw new RuntimeException("Invalid offer.")
08.else {
09.def result = offer.save()
10.recipients.each {
11.if(!it.validate()) {
12.throw new RuntimeException("Invalid recipient")
13.else {
14.offer.addToRecipients(it)
15.}
16.}
17.}
18.}
19.}

You can then catch the exception in the Controller and render a more user-friendly error message.

01.class OfferController {
, Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-
分享到:
评论

相关推荐

    Grails Grails Grails

    Grails Grails Grails Grails Grails

    Grails权威指南 Grails权威指南

    Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南

    Eclipse下搭建Grails项目

    Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。

    Grails入门指南 -- 针对grails1.0.4更新

    Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程

    Groovy轻松入门——Grails实战基础篇

    在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一--Grails相信大家早已耳闻,我将通过Grails实战系列文章 向您展现Grails的迷人风采,使您感受到Grails的魅力,以至疯狂地爱上Grails,并坠入...

    grails开发环境配置及应用开发

    详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发

    Grails1.1中文文档

    Grails1.1中文文档

    grails+Xfire webservice

    grails+Xfire webservice

    grails 1.0.4

    Grails专为下一代JavaWeb应用程序而设计的框架,其借助于Groovy动态语言,使Web开发变得简单而方便。Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一...

    grails

    grails-2.1.zip.001

    Grails中文参考手册

    Grails 中文 参考手册

    grails框架

    grails的插件系统也是其亮点之一。首先,和rails,django等web框架类似,基于微内核的思想,插件(可重用模块)是框架的一等公民。grails除了核心模块以外的功能几乎都是通过插件方式实现的。实际上,一个grails插件...

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    Grails1.3.7参考手册

    Grails 1.3.7英文版官方参考手册,学习Grails的权威指南

    grails入门经典

    grails grails入门经典 grails入门 grails例子 grails资料 通过自学一点点积累起来的,相信对你有帮助的。

    Grails1.1中文文档(CHM)

    Grails1.1最新 中文 文档 当今的Java Web开发技术显得过于复杂,相对于它本身的需要来说。现在主流的Java Web框架也是异常复杂,而且没有很好的遵循 Don't Repeat Yourself (DRY) 法则。 因此我们要以一种新的思维...

    grails中文入门简介

    比较难找的grails的资料,我也是找了很久才找到的。需要grails相关的拿走

    grails3.2.8-01

    grails3.2.8 part1

Global site tag (gtag.js) - Google Analytics