-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Long session - what if commit in the servlet-filter fails?
PostPosted: Tue Dec 20, 2005 8:46 am 
Newbie

Joined: Tue Jan 18, 2005 7:08 am
Posts: 10
I'm using the "hibernate long session" pattern as described in the book by G.King and C.Bauer (chapter 8). But the problem is: if there happens an error when I try to commit in the servlet-filter (e.g. a constraint problem), I do not know how to let the user know about the problem (with a useful error message).
I'm also using flush(), but this does not solve the problem.

Is there a solution for this problem?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 9:13 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Yes, there is a solution.

You need to wrap the response object to your own class so as to provide the filter chain with a virtual output stream. Then at commit time you can decide either to pass this as is to client or to replace it with your error message.

An example of such filtering can be found at http://www-128.ibm.com/developerworks/java/library/j-tomcat/?open&l=101,t=grj,p=TomcatTricks
See ReplaceTextFilter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 1:40 pm 
Newbie

Joined: Tue Jan 18, 2005 7:08 am
Posts: 10
Ok, I see.

But what if I'm using Struts and would like to call another action (or some other URL)?
Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 5:52 pm 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Then the best thing is to do exactly as you would do to call another servlet inside a servlet.

Use request.getRequestDispatcher("/some/path.do").forward(request, response);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 4:37 am 
Newbie

Joined: Tue Jan 18, 2005 7:08 am
Posts: 10
I already tried this way, but then I get an IllegalStateException because I already accessed the ServletOutputStream.
But whether an error happened or not I only know after that I commit to the DB (and at that time the servlet has already bagun to write to the OutputStream).

1) user wants to commit changes to the DB
2) my filter executes chain
3) the filter tries to commit


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 4:47 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
That's the reason you must provide your own ServeltOutputStream to the filter chain. Anything the filter chain does on the outputstream will only be send to the client if your filter was able to commit:
Code:
- User do a query
- Filter stores the ServletOutputStream to a local variable
- Filter Create a memory based ServletOutputStream
- Filter call filterchain providing the memory based ServletOutputStream
- filerchain do various thing that end in various outputs
- Filter tries to commit
   - if commit success, Filter copy the memory based ServletOutputStream to the locally saved ServletOutputStream
   - if commit fail, send a redirect using the locally saved ServletOutputStream, don't care about what's in the memory based ServletOutputStream
- Returns


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.