use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class AgentDaoTest method shouldCacheCookieForAgent.
@Test
public void shouldCacheCookieForAgent() throws Exception {
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
agentCookie.updateCookie("updated_cookie");
session.update(agentCookie);
return null;
}
});
assertThat(cookieForUuid(), is("updated_cookie"));
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
goCache.clear();
assertThat(agentDao.cookieFor(agentIdentifier), is("updated_cookie"));
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class AgentDaoTest method shouldNotClearCacheIfTransactionFails.
@Test
public void shouldNotClearCacheIfTransactionFails() throws Exception {
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
agentCookie.updateCookie("updated_cookie");
session.update(agentCookie);
return null;
}
});
assertThat(cookieForUuid(), is("updated_cookie"));
agentDao.setHibernateTemplate(mockHibernateTemplate);
doThrow(new RuntimeException("holy smoke")).when(mockHibernateTemplate).saveOrUpdate(any(AgentCookie.class));
try {
agentDao.associateCookie(agentIdentifier, "cookie");
fail("should have propagated saveOrUpdate exception");
} catch (Exception e) {
assertThat(e.getMessage(), is("holy smoke"));
}
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
}
use of org.springframework.orm.hibernate3.HibernateCallback in project rhino by PLOS.
the class ArticleListCrudServiceImpl method getArticleList.
private ArticleListView getArticleList(final ArticleListIdentity identity) {
Object[] result = DataAccessUtils.uniqueResult(hibernateTemplate.execute((HibernateCallback<List<Object[]>>) session -> {
Query query = queryFor(session, "select j.journalKey, l", identity);
return query.list();
}));
if (result == null) {
throw nonexistentList(identity);
}
String journalKey = (String) result[0];
ArticleList articleList = (ArticleList) result[1];
return articleListViewFactory.getView(articleList, journalKey);
}
Aggregations