Search in sources :

Example 11 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class MaterialRepository method modificationFor.

public List<Modification> modificationFor(final StageIdentifier stageIdentifier) {
    if (stageIdentifier == null) {
        return null;
    }
    String key = cacheKeyForModificationsForStageLocator(stageIdentifier);
    List<Modification> modifications = (List<Modification>) goCache.get(key);
    if (modifications == null) {
        synchronized (key) {
            modifications = (List<Modification>) goCache.get(key);
            if (modifications == null) {
                modifications = getHibernateTemplate().executeFind(new HibernateCallback() {

                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Query q = session.createQuery("FROM Modification WHERE revision = :revision ORDER BY id DESC");
                        q.setParameter("revision", stageIdentifier.getStageLocator());
                        return q.list();
                    }
                });
                if (!modifications.isEmpty()) {
                    goCache.put(key, modifications);
                }
            }
        }
    }
    return modifications;
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Example 12 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class OauthRepository method deleteUsersOauthGrants.

public void deleteUsersOauthGrants(final List<String> userIds) {
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            getHibernateTemplate().execute(new HibernateCallback() {

                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    deleteEntitiesByUserIds(OauthAuthorization.class, session, userIds);
                    deleteEntitiesByUserIds(OauthToken.class, session, userIds);
                    return true;
                }
            });
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(org.hibernate.Session)

Example 13 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class UserSqlMapDaoTest method shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB.

@Test
public void shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB() throws Exception {
    GoCache cache = mock(GoCache.class);
    UserSqlMapDao userSqlMapDaoSpy = spy(new UserSqlMapDao(sessionFactory, transactionTemplate, cache, transactionSynchronizationManager));
    doReturn(mockHibernateTemplate).when(userSqlMapDaoSpy).hibernateTemplate();
    doReturn(10).when(mockHibernateTemplate).execute(Matchers.<HibernateCallback<Object>>any());
    Integer firstEnabledUserCount = userSqlMapDaoSpy.enabledUserCount();
    assertThat(firstEnabledUserCount, is(10));
    verify(mockHibernateTemplate, times(1)).execute(any(HibernateCallback.class));
    verify(cache, times(2)).get(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY);
    verify(cache, times(1)).put(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY, 10);
}
Also used : GoCache(com.thoughtworks.go.server.cache.GoCache) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Test(org.junit.Test)

Example 14 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class PipelineRepositoryTest method stubPipelineInstancesInDb.

private void stubPipelineInstancesInDb(Object[]... rows) {
    pipelineRepository.setHibernateTemplate(new HibernateTemplate() {

        @Override
        public <T> T execute(HibernateCallback<T> action) throws DataAccessException {
            try {
                return action.doInHibernate(session);
            } catch (SQLException e) {
                throw new RuntimeException();
            }
        }
    });
    when(session.createSQLQuery(anyString())).thenReturn(sqlQuery);
    when(sqlQuery.list()).thenReturn(Arrays.asList(rows));
}
Also used : SQLException(java.sql.SQLException) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 15 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class ScheduleTestUtil method modForRev.

private Modification modForRev(final String revision) {
    return (Modification) materialRepository.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.createQuery("from Modification where revision = ?");
            q.setParameter(0, revision);
            List list = q.list();
            if (list.isEmpty()) {
                throw new RuntimeException("you are trying to load revision " + revision + " which doesn't exist");
            }
            return list.get(0);
        }
    });
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session)

Aggregations

HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)31 Session (org.hibernate.Session)19 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 Query (org.hibernate.Query)9 List (java.util.List)8 SQLException (java.sql.SQLException)7 Criteria (org.hibernate.Criteria)6 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 Test (org.junit.Test)4 Agent (com.thoughtworks.go.server.domain.Agent)3 BigInteger (java.math.BigInteger)3 Map (java.util.Map)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 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)2 HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)2 RunIf (com.googlecode.junit.ext.RunIf)1