Use Session.exists() to check whether there is a Session
--------------------------------------------------------
Key: WICKET-4174
URL: https://issues.apache.org/jira/browse/WICKET-4174
Project: Wicket
Issue Type: Bug
Components: wicket
Affects Versions: 1.5.2
Reporter: Dirk Forchel
Priority: Critical
Changes made in revision 1186125 are not complete and may affect applications
with custom RequestCycleListeners and custom WebRequests in case stateless
WebPages and ResourceReferences are used. There are no problems with stateful
WebPages because the used Session object is stored in the SessionStore with an
appropriate Id.
In our case, the overridden getLocale()-method in out WebRequest needs a
database-connection to retrieve a Language-item and its Locale. The language
ISO-code is retrieved from the request parameters (e.g.
http://localhost/de/foo/foo.html or http://localhost/en/foo/foo.html).
Therefore our custom RequestCycleListener establishes a connection to the
database. The transaction is started in the onBeginRequest()-method and
commited in the onEndRequest()-method.
The problem can be found in the Application class and the
AbstractRequestCycleListener and its onDetach()-method. This method is called
AFTER the onEndRequest()-method of our custom RequestCycleListener but the
transaction is already closed for this request. In that case, creating a new
Session object with Session.get() throws an IllegalStateException in our
application (see example StackTrace below).
public final RequestCycle createRequestCycle(final Request request, final
Response response)
{
RequestCycleContext context = new RequestCycleContext(request,
response,
getRootRequestMapper(),
getExceptionMapperProvider().get());
RequestCycle requestCycle =
getRequestCycleProvider().get(context);
requestCycle.getListeners().add(new
AbstractRequestCycleListener()
{
@Override
public void onDetach(final RequestCycle requestCycle)
{
Session.get().getPageManager().commitRequest();
}
@Override
public void onEndRequest(RequestCycle cycle)
{
if (Application.exists())
{
IRequestLogger requestLogger =
Application.get().getRequestLogger();
if (requestLogger != null)
{
requestLogger.requestTime((System.currentTimeMillis() - cycle.getStartTime()));
}
}
}
});
requestCycle.getListeners().add(requestCycleListeners);
return requestCycle;
}
Example StackTrace:
java.lang.IllegalStateException: there is no transaction bound to this thread
at org.test.framework.Model.currentTransaction(Model.java:604)
at org.test.framework.Query.search(Query.java:479)
at org.test.framework.Query.searchSingleton(Query.java:704)
at org.test.framework.Type.searchSingleton(Type.java:1049)
at org.test.framework.FunctionField.searchUnique(FunctionField.java:317)
at org.test.model.Language.forIsoCode(Language.java:136)
at
org.test.foo.FooServletWebRequest.retrieveLanguageFromRequest(FooServletWebRequest.java:159)
at
org.test.foo.FooServletWebRequest.getLanguage(FooServletWebRequest.java:120)
at
org.test.foo.FooServletWebRequest.getLocale(FooServletWebRequest.java:42)
at org.apache.wicket.Session.<init>(Session.java:215)
at org.apache.wicket.protocol.http.WebSession.<init>(WebSession.java:96)
at org.test.foo.FooSession.<init>(FooSession.java:49)
at org.test.foo.FooApplication.newSession(FooApplication.java:110)
at
org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1487)
at org.apache.wicket.Session.get(Session.java:159)
at org.apache.wicket.Application$2.onDetach(Application.java:1552)
at
org.apache.wicket.request.cycle.RequestCycleListenerCollection$3.notify(RequestCycleListenerCollection.java:101)
at
org.apache.wicket.request.cycle.RequestCycleListenerCollection$3.notify(RequestCycleListenerCollection.java:96)
at
org.apache.wicket.util.listener.ListenerCollection.reversedNotify(ListenerCollection.java:121)
at
org.apache.wicket.util.listener.ListenerCollection.reversedNotify(ListenerCollection.java:112)
at
org.apache.wicket.request.cycle.RequestCycleListenerCollection.onDetach(RequestCycleListenerCollection.java:95)
at
org.apache.wicket.request.cycle.RequestCycle.onDetach(RequestCycle.java:569)
at
org.apache.wicket.request.cycle.RequestCycle.detach(RequestCycle.java:508)
at
org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:284)
at
org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:162)
at
org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:218)
To fix the problem, the AbstractRequestCycleListener in the Application class
must check first whether there is a Session or not.
@Override
public void onDetach(final RequestCycle requestCycle)
{
if (Session.exists())
{
Session.get().getPageManager().commitRequest();
}
}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira