Hi guys,
I'm using request factory with single Hibernate session per request using
FilterServlet implementation as described here
http://stackoverflow.com/questions/4988397/gwt-requestfactory-how-to-use-single-entitymanager-per-request.
These are the two classes:
public class ThreadLocalHibernate
{
private static ThreadLocal<Session> holder = new ThreadLocal<Session>();
private ThreadLocalHibernate()
{
}
public static Session get()
{
return holder.get();
}
public static void set(Session session)
{
holder.set(session);
}
}
public class PersistenceFilter implements Filter {
protected static final Logger log =
Logger.getLogger(PersistenceFilter.class.getName());
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Avvia Hibernate
MyHibernateUtil.getSessionFactory();
}
@Override
public void destroy() {
// Ferma Hibernate
MyHibernateUtil.getSessionFactory().close();
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
Session session =
MyHibernateUtil.getSessionFactory().getCurrentSession();
ThreadLocalHibernate.set(session);
try {
// Inizia la transazione
session.beginTransaction();
chain.doFilter(req, res);
// Chiude la transazione
session.flush();
session.getTransaction().commit();
} catch (Throwable ex) {
log.error("", ex);
try {
if (session != null && session.getTransaction() != null &&
session.getTransaction().isActive()) {
session.getTransaction().rollback();
}
} catch (RuntimeException rbEx) {
log.error("Non riesco ad annullare la transazione.", rbEx);
}
String message = "";
if (ex.getCause() != null)
message = ex.getCause().getMessage();
else
message = ex.getMessage();
throw new IOException(message);
}
}
}
I can't send to the clients exceptions that are rised in doFilter() methods
of PersistenceFilter. In fact many errors are launch only when the row
session.getTransaction().commit() is run. So actually I see an error in the
server but on the client I see a success message.
Thanks for your help
Best regards
Il giorno domenica 27 marzo 2011 22:36:58 UTC+2, frog ha scritto:
>
>
> I think filters were designed for this kind of job
> Have you seen this?
>
> http://stackoverflow.com/questions/4988397/gwt-requestfactory-how-to-use-single-entitymanager-per-request
> <http://stackoverflow.com/questions/4988397/gwt-requestfactory-how-to-use-single-entitymanager-per-request>
>
> On Sun, Mar 27, 2011 at 7:27 PM, rwiatr <[email protected] <javascript:>>wrote:
>
>> Thanks for the response Nooonika.
>> Unfortunately it doesn't work for me. I'm looking for a mechanism to
>> open a session before request is processed and close after (or
>> something like that).
>> Here's something that works, at least for now.
>>
>> public class SessionRequestFactoryServlet extends
>> RequestFactoryServlet {
>> private static final ThreadLocal<Session> perThreadSession = new
>> ThreadLocal<Session>();
>>
>> @Override
>> protected void doPost(HttpServletRequest request,
>> HttpServletResponse response) throws IOException,
>> ServletException
>> {
>>
>> perThreadSession.set(HibernateUtil.getSessionFactory().openSession());
>> try {
>> super.doPost(request, response);
>> } finally {
>> perThreadSession.get().close();
>> perThreadSession.set(null);
>> }
>> }
>>
>> public static Session getPerThreadSession() {
>> return perThreadSession.get();
>> }
>> }
>> in the DAO object:
>> @Transient
>> public static List<HierarchyImpl> rootHierarchyRequest() {
>> try {
>> Session session = SessionRequestFactoryServlet
>> .getPerThreadSession();
>> List<HierarchyImpl> hList = new
>> ArrayList<HierarchyImpl>(session
>> .createQuery("from Hierarchy where
>> parent is null").list());
>> return hList;
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>> return null;
>> }
>> And a servlet entry in web.xml
>> <servlet-class>app.server.requestfactory.SessionRequestFactoryServlet</
>> servlet-class>
>>
>> RequestFactoryServet uses ThreadLocal variables so I guess it's ok.
>> Anyone knows a more suitable way to achieve this? I don't have any
>> experience with such thing and I'm afraid it can cause much more
>> problems in the future. Anyone has any suggestions?
>>
>> On 27 Mar, 16:20, Nooonika <[email protected]> wrote:
>> > Try: Hibernate.initialize(hList);
>> >
>> > On Mar 27, 11:47 am, rwiatr <[email protected]> wrote:
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > > Hello,
>> > > I have a problem with RequestFactory and Hibernate session.
>> > > [hibernate.LazyInitializationException] - failed to lazily initialize
>> > > a collection of role: app.server.dao.HierarchyImpl.children, no
>> > > session or session was closed
>> >
>> > > @OneToMany(cascade = CascadeType.ALL)
>> > > @JoinTable(name = "HIERARCHY_RELATE", joinColumns =
>> > > { @JoinColumn(name = "parent_id") }, inverseJoinColumns =
>> > > { @JoinColumn(name = "child_id") })
>> > > public Set<HierarchyImpl> getChildren() {
>> > > return children;
>> > > }
>> >
>> > > public static List<HierarchyImpl> rootHierarchyRequest() {
>> > > int i = 0;
>> > > try {
>> > > SessionFactory sessionFactory =
>> HibernateUtil.getSessionFactory();
>> > > Session session =
>> sessionFactory.openSession();
>> > > List<HierarchyImpl> hList = new
>> ArrayList<HierarchyImpl>(session
>> > > .createQuery("from Hierarchy
>> where parent is null").list());
>> > > session.close();
>> > > return hList;
>> > > } catch (Exception e) {
>> > > e.printStackTrace();
>> > > }
>> > > return null;
>> > > }
>> > > Adding FetchType.EAGER helps but in long terms will not solve my
>> > > problem.
>> > > Is there a good way to manage Hibernate session by RequestFactory?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Google Web Toolkit" group.
>> To post to this group, send email to
>> [email protected]<javascript:>
>> .
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>.
>> For more options, visit this group at
>> http://groups.google.com/group/google-web-toolkit?hl=en.
>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-web-toolkit/-/elbqwodpGtAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.