Näytetään tekstit, joissa on tunniste spring. Näytä kaikki tekstit
Näytetään tekstit, joissa on tunniste spring. Näytä kaikki tekstit

maanantai 20. syyskuuta 2010

c3p0, Spring, Hibernate and NoResultException

I was fighting for a while with NoResultException and c3p0:s config because I thought that was the problem with NoResultExceptions I got. While making uutispuro.fi I finally realized it wasn't the source of the problem.

I had a method doing a persist and a method doing a merge which I had surrounded with try and catch ... and of course with a finally block doing an em.close().

The problem was with the queries doing a .getSingleResult(). I forgot that these might throw the error in hand, and if they did, the closing of entitymanager wasn't done.


try {
Query query = em.createQuery("select query");
return query.getSingleResult();
} catch (NoResultException e) {
log.debug(e);
} catch (Exception e) {
log.error(e);
} finally {
em.close();
}

maanantai 6. heinäkuuta 2009

Spring controller and redirecting

Two ways to use redirecting. First one uses the url mapping configured in an xml file. The url seems to be the same for the client (or actually is, depends on the situation). The second one tells the client to redirect to a certain page, so the url in the client changes accordingly.

1. Use the Spring url mapping
Map model = new HashMap();
model.put("article_id", "get it from db?");
return new ModelAndView("article", model);


2. Tell the client to go to a certain page
return new ModelAndView(new RedirectView("index.jsp"));