KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > TransactionFilter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.filters;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.servlet.Filter JavaDoc;
35 import javax.servlet.FilterChain JavaDoc;
36 import javax.servlet.FilterConfig JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletResponse JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41 import java.io.IOException JavaDoc;
42
43 /**
44  * Wraps the request in a transaction. All database calls for
45  * the request will either succeed together or fail.
46  *
47  * @since Resin 2.0.5
48  */

49 public class TransactionFilter implements Filter JavaDoc {
50   /**
51    * The UserTransaction object.
52    */

53   private UserTransaction JavaDoc _userTransaction;
54   
55   /**
56    * Lookup java:comp/UserTransaction and cache the results.
57    */

58   public void init(FilterConfig JavaDoc config)
59     throws ServletException JavaDoc
60   {
61     try {
62       Context JavaDoc ic = (Context JavaDoc) new InitialContext JavaDoc();
63       
64       _userTransaction = (UserTransaction JavaDoc) ic.lookup("java:comp/UserTransaction");
65     } catch (NamingException JavaDoc e) {
66       throw new ServletException JavaDoc(e);
67     }
68   }
69   
70   /**
71    * Wrap the request in a transaction. If the request returns normally,
72    * the transaction will commit. If an exception is thrown it will
73    * rollback.
74    */

75   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
76                        FilterChain JavaDoc nextFilter)
77     throws ServletException JavaDoc, IOException JavaDoc
78   {
79     try {
80       _userTransaction.begin();
81
82       nextFilter.doFilter(request, response);
83
84       _userTransaction.commit();
85     } catch (ServletException JavaDoc e) {
86       rollback();
87       throw e;
88     } catch (IOException JavaDoc e) {
89       rollback();
90       throw e;
91     } catch (RuntimeException JavaDoc e) {
92       rollback();
93       throw e;
94     } catch (Throwable JavaDoc e) {
95       rollback();
96       throw new ServletException JavaDoc(e);
97     }
98   }
99
100   /**
101    * Rolls the request back.
102    */

103   private void rollback()
104     throws ServletException JavaDoc
105   {
106     try {
107       _userTransaction.rollback();
108     } catch (Exception JavaDoc e) {
109       throw new ServletException JavaDoc(e);
110     }
111   }
112   
113   /**
114    * Any cleanup for the filter.
115    */

116   public void destroy()
117   {
118   }
119 }
120
Popular Tags