Search in sources :

Example 26 with HibernateCallback

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"));
}
Also used : HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session) AgentCookie(com.thoughtworks.go.server.domain.AgentCookie) Test(org.junit.Test)

Example 27 with HibernateCallback

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"));
}
Also used : HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) AgentCookie(com.thoughtworks.go.server.domain.AgentCookie) Test(org.junit.Test)

Example 28 with HibernateCallback

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);
}
Also used : Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) ArticleList(org.ambraproject.rhino.model.ArticleList)

Aggregations

HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)27 Session (org.hibernate.Session)15 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 SQLException (java.sql.SQLException)7 Query (org.hibernate.Query)6 List (java.util.List)5 Criteria (org.hibernate.Criteria)5 Test (org.junit.Test)4 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)4 AgentCookie (com.thoughtworks.go.server.domain.AgentCookie)3 BigInteger (java.math.BigInteger)3 HibernateException (org.hibernate.HibernateException)3 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)2 ArrayList (java.util.ArrayList)2 SQLQuery (org.hibernate.SQLQuery)2 LongType (org.hibernate.type.LongType)2 RunIf (com.googlecode.junit.ext.RunIf)1 AbstractMaterial (com.thoughtworks.go.config.materials.AbstractMaterial)1 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)1 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)1