Return-Path: Delivered-To: apmail-struts-user-archive@www.apache.org Received: (qmail 16521 invoked from network); 13 Nov 2008 20:32:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Nov 2008 20:32:10 -0000 Received: (qmail 28895 invoked by uid 500); 13 Nov 2008 20:32:06 -0000 Delivered-To: apmail-struts-user-archive@struts.apache.org Received: (qmail 28871 invoked by uid 500); 13 Nov 2008 20:32:06 -0000 Mailing-List: contact user-help@struts.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Struts Users Mailing List" Reply-To: "Struts Users Mailing List" Delivered-To: mailing list user@struts.apache.org Received: (qmail 28860 invoked by uid 99); 13 Nov 2008 20:32:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Nov 2008 12:32:06 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of burtonrhodes@gmail.com designates 216.239.58.188 as permitted sender) Received: from [216.239.58.188] (HELO gv-out-0910.google.com) (216.239.58.188) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Nov 2008 20:30:45 +0000 Received: by gv-out-0910.google.com with SMTP id c6so249117gvd.25 for ; Thu, 13 Nov 2008 12:31:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=2gjSppGZcFZq6j7Cj3aCa7vbxv10a9YwnqiF4/G8ATQ=; b=thdVN49x9QUClkAme/BcDtSkMDhHPFTx7UUPXjtRLv0Ppij1oQyow/469uEg0L4Iae WU9QKxeoP6Eblx+hBOY3hXXtuFb16eIBGMCd03f6VybU4aVdHORYZ6/XR85ez1oSK3tk dKOfv7KIJgzNc/AN2eIWMGBd1nVlLG67QAh1Y= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=kAX1A+K/Htx01uqqVegdOgFSG1AzIULWiDXq3erkw928lV8TrKqJCeXVAvdm/Rk6a/ 7RGuceSnQzo0+Qlcn7HMh6grt6Lcr38zhpr+cMX0byAngkCmEERQro4CtOgnnCPil96n FzEEHXLTgc0C4egHLCe9ImP0Kf47dbC93U5Mo= Received: by 10.103.92.8 with SMTP id u8mr56265mul.34.1226608277674; Thu, 13 Nov 2008 12:31:17 -0800 (PST) Received: by 10.103.137.12 with HTTP; Thu, 13 Nov 2008 12:31:17 -0800 (PST) Message-ID: <3017fd9e0811131231g5c885644ke8911fcb1614943e@mail.gmail.com> Date: Thu, 13 Nov 2008 14:31:17 -0600 From: "Burton Rhodes" To: "Struts Users Mailing List" Subject: Using validation with Hibernate... MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Checked: Checked by ClamAV on apache.org I am having trouble getting Struts 2.x to implement validation and hibernate using the HibernateSessionRequestFilter (the pattern suggested on Hibernate.org). When the validation fails, Hibernate will still update the object in the database when the HibernateFilter closes the transaction. I get why this is happening, but I can't seem to find anyone who has really figured out an *elegant* solution. Any ideas? public class HibernateSessionRequestFilter implements Filter { static Logger log = Logger.getLogger(HibernateSessionRequestFilter.class); private SessionFactory sf; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { // This starts the Hibernate transaction sf.getCurrentSession().beginTransaction(); // Call the next filter (continue request processing) chain.doFilter(request, response); // Commit and cleanup sf.getCurrentSession().getTransaction().commit(); <---- ********* Data is written here no matter what ************ } catch (StaleObjectStateException staleEx) { log.fatal("This interceptor does not implement optimistic concurrency control!"); log.fatal("Your application will not work until you add compensation actions!"); // Rollback, close everything, possibly compensate for any permanent changes // during the conversation, and finally restart business conversation. Maybe // give the user of the application a chance to merge some of his work with // fresh data... what you do here depends on your applications design. throw staleEx; } // This is a catch-all catch, define other Exceptions here catch (Throwable ex) { // Rollback only ex.printStackTrace(); try { if (sf.getCurrentSession().getTransaction().isActive()) { log.fatal("Trying to rollback database transaction after exception"); sf.getCurrentSession().getTransaction().rollback(); } } catch (Throwable rbEx) { log.fatal("Could not rollback transaction after exception!", rbEx); } // Let others handle it... maybe another interceptor for exceptions? throw new ServletException(ex); } } public void init(FilterConfig filterConfig) throws ServletException { log.trace("Initializing filter..."); log.trace("Obtaining SessionFactory from static HibernateUtil singleton..."); sf = HibernateUtil.getSessionFactory(); } public void destroy() {} } --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@struts.apache.org For additional commands, e-mail: user-help@struts.apache.org