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 {
相关推荐
**Grails 框架详解** Grails 是一个基于 Groovy 语言的开源Web应用程序框架,它构建在Java平台之上,旨在简化开发过程并提高生产力。Grails 的设计深受Ruby on Rails的影响,提供了MVC(模型-视图-控制器)架构模式...
【Grails项目搭建详解】 Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,尤其适合快速构建动态网站。在Eclipse中搭建Grails项目可能相对复杂,但通过以下步骤,即使是初学者也能顺利进行。 1. *...
《Grails权威指南》是一本全面深入探讨Grails框架的专著,旨在帮助读者掌握这一强大的Web开发工具。Grails是一种基于Groovy语言的开源框架,它为构建现代、高效的应用程序提供了简洁高效的解决方案。本指南针对不同...
《Grails用户手册》 Grails,作为一个基于Groovy语言的开源Web应用框架,深受开发者喜爱,它简化了Java开发的复杂性,提供了强大的MVC(Model-View-Controller)架构,以及丰富的插件系统。这份用户手册将帮助你...
对于Grails开发,我们需要的是Eclipse中的Grails插件,它能够提供对Grails项目的创建、运行、调试等一系列功能。 **Grails**是基于Groovy语言的全栈式Web开发框架,它借鉴了Ruby on Rails的设计理念,提供了快速...
Grails是一个基于Groovy语言的全栈框架,它遵循约定优于配置的原则,并且紧密集成Spring和Hibernate等流行的Java库,简化了开发流程。Grails在IT行业中尤其受到重视,因为它能够帮助开发者快速搭建并部署基于MVC模式...
### Grails 快速开发 Web 应用程序 #### 一、Grails 概述 Grails 是一种基于 Groovy 的开源应用框架,用于简化 Web 应用程序的开发过程。它采用约定优于配置的原则,这使得开发者可以更快地创建功能丰富的 Web ...
《Grails 2 的终极指南》是一本深入探讨Grails框架精髓的专业书籍,该书以英文撰写,旨在为读者提供全面、深入的Grails框架学习资料。Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了...
Eclipse 插件 Grails(Groovy)是一个强大的开发工具,它使得在Eclipse环境中进行Groovy和Grails应用的开发变得更为便捷。Groovy是一种动态、面向对象的编程语言,而Grails则是一个基于Groovy的开源Web应用框架,...
**Grails 概述** Grails 是一个基于 Groovy 语言的开源 web 应用程序框架,它构建在 Java 平台上,旨在提高开发效率,简化常见 Web 开发任务。Grails 遵循 Model-View-Controller (MVC) 架构模式,允许开发者快速...
《Grails 2.4.4 框架深度解析》 Grails 2.4.4 是一个基于Java的开源Web应用框架,它利用Groovy语言的强大特性,为开发者提供了一种高效、灵活的开发环境。这个压缩包“grails-2.4.4.zip”包含了完整的Grails 2.4.4...
**Grails登录系统详解** Grails是一个基于Java的开源Web应用程序框架,它使用Groovy语言进行开发,提供了高效、简洁的编程模型。在Grails中实现用户登录功能是构建任何Web应用的基础,它确保了数据的安全性和用户...
### Groovy和Grails配置方法 #### 一、Groovy与Grails简介 Groovy是一种强大的面向对象编程语言,它运行在Java平台上,并且能够直接与Java代码进行交互。Groovy支持函数式编程特性,拥有丰富的语法糖以及简洁的...
### Grails入门指南知识点 #### 一、Grails框架简介 - **背景**: Grails是一个基于Groovy语言的开源Web应用框架,适用于Java平台。它旨在简化开发过程,提高开发效率,尤其受到那些希望保留Java环境同时寻求更高效...
《Grails技术详解:中文文档与Fckeditor-0.9.5插件解析》 Grails,作为一款基于Groovy语言的开源Web应用框架,以其高效、简洁的特性深受开发者喜爱。它集成了许多Java EE的功能,同时简化了开发流程,使得开发人员...
《Grails中文参考手册》是针对Groovy编程语言构建的Web应用框架——Grails的一份详尽学习资料。Grails以其高效、灵活和强大的特性,成为开发人员在Java平台上构建Web应用的热门选择。这份手册旨在帮助初学者快速上手...
《Grails 1.1 中文文档》是一个非常宝贵的资源,尤其对于国内的开发者来说,由于Grails在中文社区中的资料相对较少,这份文档的价值不言而喻。Grails是一个基于Groovy语言的开源Web应用框架,它借鉴了Ruby on Rails...
《Grails核心源码解析——基于版本2.2.2》 Grails,作为一个基于Groovy语言的全栈式Web应用框架,深受开发者喜爱。它以其简洁的语法、丰富的插件系统以及对Spring和Hibernate的无缝集成,为开发高效、灵活的Web应用...