You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Patrick Heiden <pa...@gmx.de> on 2008/04/17 18:00:50 UTC

[HELP] SitemapEvenListener

Hello together!

I am trying to implement an OpenSessionInViewInterceptor (OSIVI) using Cocoons (2.2) Enter/LeaveSitemapEventListener. I have one class wich implements both Listener interfaces:

...

public void enteredSitemap(EnterSitemapEvent event) {
   log.debug("Opening Hibernate Session and beginning transaction.");
   Session session = sessionFactory.openSession();
   session.beginTransaction();

   TransactionSynchronizationManager.bindResource(
             sessionFactory, new SessionHolder(session));
   TransactionSynchronizationManager.initSynchronization();
}

...

public void leftSitemap(LeaveSitemapEvent event) {
   Session session = sessionFactory.getCurrentSession();
   try {
      log.debug("Committing the database transaction.");
      session.getTransaction().commit();
   } catch ( RuntimeException ex ) {
      try {
         log.error( ex );
         log.debug("Rolling back the database transaction.");
         session.getTransaction().rollback();
         if ( session.isOpen() ) {
            session.close();
         }
      } catch ( RuntimeException e ) {
         log.error( e );
         throw e; // Let the exception propagate up the stack.
      }
   } finally {
      try {
         if ( session.isOpen() ) {
            session.close();
         }
      } catch ( RuntimeException e ) { /* do nothing */ }
      TransactionSynchronizationManager.unbindResource( sessionFactory );
      TransactionSynchronizationManager.clearSynchronization();
   }
}

...

I thought this would work, since i manually bind the session to the 'current' thread. But after opening the session (and some DB Work wich is logged to stdout) I get following Exception from out of leftSitemap( ... ):

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
        at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
        at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
        at de.ifado.isac.web.OpenSessionInViewInterceptor.leftSitemap(OpenSessionInViewInterceptor.java:70)
        at org.apache.cocoon.components.treeprocessor.ConcreteTreeProcessor.process(ConcreteTreeProcessor.java:256)
        at org.apache.cocoon.components.treeprocessor.ConcreteTreeProcessor.process(ConcreteTreeProcessor.java:171)
        at org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcessor.java:247)
        at org.apache.cocoon.servlet.RequestProcessor.process(RequestProcessor.java:351)
        at org.apache.cocoon.servlet.RequestProcessor.service(RequestProcessor.java:169)
        at org.apache.cocoon.sitemap.SitemapServlet.service(SitemapServlet.java:84)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:468)
        at org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:443)
        at org.apache.cocoon.servletservice.spring.ServletFactoryBean$ServiceInterceptor.invoke(ServletFactoryBean.java:264)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy5.service(Unknown Source)
        at org.apache.cocoon.servletservice.DispatcherServlet.service(DispatcherServlet.java:106)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
        at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
        at org.apache.cocoon.servlet.multipart.MultipartFilter.doFilter(MultipartFilter.java:131)
        at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
        at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
        at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
        at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
        at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
        at org.mortbay.jetty.Server.handle(Server.java:324)
        at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
        at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:828)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
        at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
        at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
        at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:450)

I have registered the Listener throug a bean definition inside my blocks COB-INF/config/spring/listener.xml:

<bean id="openSessionInViewInterceptor" class="de.ifado.isac.web.OpenSessionInViewInterceptor">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>

The SessionFactory is defined elsewhere (and retrieved after global WebAppCtx is build through Cocoon/Spring.

I have no idea why my sessionContext gets lost!?

Help appreciated,
greetings,
Patrick
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Joerg Heinicke <jo...@gmx.de>.
On 23.04.2008 05:48, Patrick Heiden wrote:

> Still no ideas/solutions to share (Jörg?). 

I can't hide, can I? ;)

>> A have read [1] and assume that one of your aforementioned colleagues is
>> Jörg Heinicke ( maybe you have a solution meanwhile, Jörg? Especially: How
>> are you able to use declarative transaction demarciation within this
>> jungle? ;) )

Declarative transaction demarcation indeed used to work nicely for me. 
But I have never tried it in the context of Cocoon. And it's now a year 
ago that I worked with that stuff the last time.

You need at least to elaborate a bit more. Maybe I'm still able to help 
you :)

Joerg

>> [1] http://forum.springframework.org/showthread.php?t=32270&page=2

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Patrick Heiden <pa...@gmx.de>.
Hello!

<snip/>
Still no ideas/solutions to share (Jörg?). 


> I am still within this case ;)
> A have read [1] and assume that one of your aforementioned colleagues is
> Jörg Heinicke ( maybe you have a solution meanwhile, Jörg? Especially: How
> are you able to use declarative transaction demarciation within this
> jungle? ;) )
> 

<snip/>


Greetings,
Patrick
> [1] http://forum.springframework.org/showthread.php?t=32270&page=2

-- 
Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games! 
http://games.entertainment.gmx.net/de/entertainment/games/free

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Patrick Heiden <pa...@gmx.de>.
Hi Carsten!

<snip/>

> No, that's the correct place. Actually I have no idea why this is not 
> working for you. As the place is correct and as the listener is called 
> on enter and exit, I assume that the problem is somewhere in your code.

I am still within this case ;)
A have read [1] and assume that one of your aforementioned colleagues is Jörg Heinicke ( maybe you have a solution meanwhile, Jörg? Especially: How are you able to use declarative transaction demarciation within this jungle? ;) )

It must be (and is) inside my hibernate code. As most 'naive' beginners, trying to implement session-per-request and/or long-conversation, I thought that (as stated like golden rain inside spring-docs) coupling hibernate and spring is easy. But it isn't THAT easy with web-applications. I think, that springs LocalSessionFactoryBean represents hibernate sessions in a manner that don't consider such patterns much (e.g. a call to getCurrentSession() doesn't guarantee to adopt MY actual scope of persistence-context associated with Hibernate Sessions, leading to frustated users adopting 'outdated' programmatic transaction-management).

So I am still appreciative for any hints.

Greetings,
Patrick

[1] http://forum.springframework.org/showthread.php?t=32270&page=2
-- 
Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games! 
http://games.entertainment.gmx.net/de/entertainment/games/free

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Carsten Ziegeler <cz...@apache.org>.
Patrick Heiden wrote:
> Hello Carsten!
> 
>> I have no knowledge about this spring/hibernate stuff, but if your 
>> hibernate code is correct, it should work (ok, I know this doesn't help 
>> you). 
> 
>> So, my question is if you're sure that your code in leftSitemap() 
>> should really get to the session created in enteredSitemap?. 
> 
> Could you be more pricise on what you mean, I don't understand exactly. Do you want to explain, that my hibernate-cleanup code shouldn't be placed inside leftSitemap() ?
> 
No, that's the correct place. Actually I have no idea why this is not 
working for you. As the place is correct and as the listener is called 
on enter and exit, I assume that the problem is somewhere in your code.


Carsten

-- 
Carsten Ziegeler
cziegeler@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Patrick Heiden <pa...@gmx.de>.
Hello Carsten!

> I have no knowledge about this spring/hibernate stuff, but if your 
> hibernate code is correct, it should work (ok, I know this doesn't help 
> you). 

> So, my question is if you're sure that your code in leftSitemap() 
> should really get to the session created in enteredSitemap?. 

Could you be more pricise on what you mean, I don't understand exactly. Do you want to explain, that my hibernate-cleanup code shouldn't be placed inside leftSitemap() ?

> Could it be that internal cocoon requests are used and leftSitemap is 
> called twice?

No, there no internal requests inside my actual 'TestingCode' so far. But I would try to propagate Hibernates Session trough such internal calls and don't register a listener on internal called sitemaps. You could imagine the interceptor beeing registered to a sitemap wich also hosts a main Controller for my domain model (via flowscript). Transaction boundaries would be on that level and further internal calls would be inside an already opened Session. That is what I thought is possible from theory ([1], [2]). 

> I remember that collegues of mine did the same and it worked for them.
> 
> HTH
> Carsten

Why they don't publish their results as Cocoon-component / bean ? ;)
 
<snip/>

Greetings,
Patrick

[1] http://www.nabble.com/C2.2-and-OpenSessionInViewInterceptor-td14229133.html

[2] http://blogs.ittoolbox.com/java/lessons/archives/implementing-hibernates-sessionperrequest-pattern-using-spring-mvc-16398
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [HELP] SitemapEvenListener

Posted by Carsten Ziegeler <cz...@apache.org>.
I have no knowledge about this spring/hibernate stuff, but if your 
hibernate code is correct, it should work (ok, I know this doesn't help 
you). So, my question is if you're sure that your code in leftSitemap() 
should really get to the session created in enteredSitemap?. Could it be 
that internal cocoon requests are used and leftSitemap is called twice?
I remember that collegues of mine did the same and it worked for them.

HTH
Carsten


Patrick Heiden wrote:
> Hello together!
> 
> I am trying to implement an OpenSessionInViewInterceptor (OSIVI) using Cocoons (2.2) Enter/LeaveSitemapEventListener. I have one class wich implements both Listener interfaces:
> 
> ...
> 
> public void enteredSitemap(EnterSitemapEvent event) {
>    log.debug("Opening Hibernate Session and beginning transaction.");
>    Session session = sessionFactory.openSession();
>    session.beginTransaction();
> 
>    TransactionSynchronizationManager.bindResource(
>              sessionFactory, new SessionHolder(session));
>    TransactionSynchronizationManager.initSynchronization();
> }
> 
> ...
> 
> public void leftSitemap(LeaveSitemapEvent event) {
>    Session session = sessionFactory.getCurrentSession();
>    try {
>       log.debug("Committing the database transaction.");
>       session.getTransaction().commit();
>    } catch ( RuntimeException ex ) {
>       try {
>          log.error( ex );
>          log.debug("Rolling back the database transaction.");
>          session.getTransaction().rollback();
>          if ( session.isOpen() ) {
>             session.close();
>          }
>       } catch ( RuntimeException e ) {
>          log.error( e );
>          throw e; // Let the exception propagate up the stack.
>       }
>    } finally {
>       try {
>          if ( session.isOpen() ) {
>             session.close();
>          }
>       } catch ( RuntimeException e ) { /* do nothing */ }
>       TransactionSynchronizationManager.unbindResource( sessionFactory );
>       TransactionSynchronizationManager.clearSynchronization();
>    }
> }
> 
> ...
> 
> I thought this would work, since i manually bind the session to the 'current' thread. But after opening the session (and some DB Work wich is logged to stdout) I get following Exception from out of leftSitemap( ... ):
> 
> org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
>         at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
>         at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
>         at de.ifado.isac.web.OpenSessionInViewInterceptor.leftSitemap(OpenSessionInViewInterceptor.java:70)
>         at org.apache.cocoon.components.treeprocessor.ConcreteTreeProcessor.process(ConcreteTreeProcessor.java:256)
>         at org.apache.cocoon.components.treeprocessor.ConcreteTreeProcessor.process(ConcreteTreeProcessor.java:171)
>         at org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcessor.java:247)
>         at org.apache.cocoon.servlet.RequestProcessor.process(RequestProcessor.java:351)
>         at org.apache.cocoon.servlet.RequestProcessor.service(RequestProcessor.java:169)
>         at org.apache.cocoon.sitemap.SitemapServlet.service(SitemapServlet.java:84)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>         at org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:468)
>         at org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:443)
>         at org.apache.cocoon.servletservice.spring.ServletFactoryBean$ServiceInterceptor.invoke(ServletFactoryBean.java:264)
>         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
>         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
>         at $Proxy5.service(Unknown Source)
>         at org.apache.cocoon.servletservice.DispatcherServlet.service(DispatcherServlet.java:106)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>         at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
>         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
>         at org.apache.cocoon.servlet.multipart.MultipartFilter.doFilter(MultipartFilter.java:131)
>         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
>         at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
>         at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
>         at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
>         at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
>         at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
>         at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
>         at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
>         at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
>         at org.mortbay.jetty.Server.handle(Server.java:324)
>         at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
>         at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:828)
>         at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
>         at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
>         at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
>         at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
>         at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:450)
> 
> I have registered the Listener throug a bean definition inside my blocks COB-INF/config/spring/listener.xml:
> 
> <bean id="openSessionInViewInterceptor" class="de.ifado.isac.web.OpenSessionInViewInterceptor">
>    <property name="sessionFactory" ref="sessionFactory" />
> </bean>
> 
> The SessionFactory is defined elsewhere (and retrieved after global WebAppCtx is build through Cocoon/Spring.
> 
> I have no idea why my sessionContext gets lost!?
> 
> Help appreciated,
> greetings,
> Patrick


-- 
Carsten Ziegeler
cziegeler@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org