
584
DEVELOPING
COLDFUSION 9 APPLICATIONS
ColdFusion ORM
Las
t
upda
te
d 8/5/2010
Example
: expressions
This type of ORMExecuteQuery lets you retrieve data using expressions such as mathematical operators, logical
operators, binary comparisons, and many others.
For example, the following code is used to retrieve the price of an artwork, which is greater than or equal to 10000 along
with the name and description of the artwork.
<cfset artArr = ORMExecuteQuery("from Art where price>=10000")>
<cfloop array="#artArr#" index="artObj">
<cfoutput>
Art Name = #artObj.getArtName()#<br>
Description = #artObj.getDescription()#<br>
Price = #artObj.getPrice()#<br>
</cfoutput>
<br>
</cfloop>
Transaction and concurrency
When ORM methods are invoked without any transaction, all the data is committed to the database when the ORM
session is flushed. ORM session is flushed when
ORMFlush()
is called or if autoflush is enabled when the request ends.
This works fine when there is not much concurrency, however in most practical scenarios you would need to use
transaction in your application so that the data in your database is always in a consistent state.
With ColdFusion ORM, you can manage transactions in the following two ways:
•
Using Hibernate transaction:
User has full control and ColdFusion does not intervene. The application has to
flush/close the session and commit/rollback the transaction.
For more information on transactions, go to the following URL:
http://community.jboss.org/wiki/sessionsandtransactions
•
Using CFTransaction:
ColdFusion manages the transaction. Since a transaction cannot be distributed (across
different data sources), application must ensure that the changes made in the transaction affect only one Hibernate
session. That is, only one data source.
ColdFusion allows reading of data from other sessions (data source) in a transaction but changes must be made in
only one session. Multiple dirty sessions at any time in the transaction can result in exceptions and the transaction
is rolled back. Before transaction begins, all existing sessions in the request are flushed. The previous session (if any)
is reused.
When the transaction is committed, the dirty session is automatically flushed (before committing the transaction).
When the transaction is rolled back, the changed session cannot be used any longer because it can cause rolled back
data to get committed later. Therefore, the session participating in the transaction is cleared when transaction is
rolled back.
A description of transaction is beyond the scope of this document. For more information on transactions, see the
hibernate
documentation.
To run the ORM methods inside a transaction, they must be inside
<cftransaction>
. A simple example snippet of
using ORM with
<cftransaction>
is as follows:
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...