List of usage examples for org.hibernate StatelessSession beginTransaction
Transaction beginTransaction();
From source file:edu.scripps.fl.pipeline.StatelessSessionStage.java
License:Apache License
public StatelessSession getStatelessSession() { StatelessSession session = sessions.get(Thread.currentThread()); if (session == null) { session = getSessionFactory().openStatelessSession(); setStatelessSession(session);//from w w w.ja v a 2 s. c o m setTransaction(session.beginTransaction()); } return session; }
From source file:edu.utah.further.core.data.hibernate.listeners.PreUpdatePreventNullOverwriteListener.java
License:Apache License
@SuppressWarnings("resource") @Override// w w w . ja v a 2 s . c o m public boolean onPreUpdate(final PreUpdateEvent event) { if (log.isDebugEnabled()) { log.debug("Received pre-update event"); } final EntityPersister entityPersister = event.getPersister(); final EntityMode entityMode = entityPersister.guessEntityMode(event.getEntity()); final Connection connection = getConnection(entityPersister); final StatelessSession session = entityPersister.getFactory().openStatelessSession(connection); session.beginTransaction(); final Serializable entityId = entityPersister.getIdentifier(event.getEntity(), (SessionImplementor) session); final Object existingEntity = session.get(event.getEntity().getClass(), entityId); if (log.isDebugEnabled()) { log.debug("Loaded existing entity " + existingEntity); } boolean updatedExistingEntity = false; for (int i = 0; i < entityPersister.getPropertyNames().length; i++) { final Object oldPropValue = entityPersister.getPropertyValue(existingEntity, i, entityMode); if (oldPropValue == null) { if (log.isDebugEnabled()) { log.debug("Found a property in the existing " + "entity that was null, checking new entity"); } final Object newPropValue = entityPersister.getPropertyValue(event.getEntity(), i, entityMode); if (!(newPropValue instanceof Collection<?>) && newPropValue != null) { if (log.isDebugEnabled()) { log.debug("The new entity contains a non-null " + "value, updating existing entity"); } entityPersister.setPropertyValue(existingEntity, i, newPropValue, entityMode); updatedExistingEntity = true; } } } if (updatedExistingEntity) { entityPersister.getFactory().getCurrentSession().cancelQuery(); session.update(existingEntity); } session.getTransaction().commit(); session.close(); return updatedExistingEntity; }
From source file:eu.europa.ec.fisheries.uvms.spatial.service.dao.AbstractAreaDao.java
License:Open Source License
public List<Serializable> bulkInsert(Map<String, List<Property>> features, List<UploadMappingProperty> mapping) throws ServiceException { List<Serializable> invalidGeometryList = new ArrayList<>(); StatelessSession session = (getEntityManager().unwrap(Session.class)).getSessionFactory() .openStatelessSession();/* ww w .j a v a 2s . c o m*/ Transaction tx = session.beginTransaction(); try { Query query = session.getNamedQuery(getDisableAreaNamedQuery()); query.executeUpdate(); for (List<Property> properties : features.values()) { Map<String, Object> values = BaseAreaEntity.createAttributesMap(properties); BaseAreaEntity entity = createEntity(values, mapping); if (entity.getName() == null || entity.getCode() == null) { throw new ServiceException("NAME AND CODE FIELD ARE MANDATORY"); } Serializable identifier = session.insert(entity); if (!entity.getGeom().isValid()) { invalidGeometryList.add(identifier); } } log.debug("Commit transaction"); tx.commit(); } catch (Exception e) { tx.rollback(); throw new ServiceException("Rollback transaction", e); } finally { log.debug("Closing session"); session.close(); } return invalidGeometryList; }
From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java
License:Apache License
/** * session StatelessSession? ? ? , . @param session the session * * @param action the action/* w w w .j a v a 2 s .c om*/ */ public static void executeTransactional(Session session, Action1<StatelessSession> action) { if (log.isDebugEnabled()) log.debug("StatelessSession? ? Transaction ? ? ."); StatelessSession stateless = openStatelessSession(session); Transaction tx = null; try { tx = stateless.beginTransaction(); action.perform(stateless); tx.commit(); } catch (Exception e) { log.error("StatelessSession? ? ? . rollback .", e); if (tx != null) tx.rollback(); throw new RuntimeException(e); } finally { stateless.close(); } }
From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java
License:Apache License
/** * Execute transactional.//from w w w . j a v a2 s.c o m * * @param session the session * @param actions the actions */ public static void executeTransactional(Session session, Iterable<Action1<StatelessSession>> actions) { if (log.isDebugEnabled()) log.debug("StatelessSession? ? Transaction ? ? ."); StatelessSession stateless = openStatelessSession(session); Transaction tx = null; try { tx = stateless.beginTransaction(); for (Action1<StatelessSession> action : actions) { action.perform(stateless); } tx.commit(); } catch (Exception e) { log.error("StatelessSession? ? . rollback .", e); if (tx != null) tx.rollback(); throw new RuntimeException(e); } finally { stateless.close(); } }
From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java
License:Open Source License
/** * Execute a batch callback against a StatelessSession using a named query. * /* w w w. ja v a 2s. c om*/ * <p>The DELETE, UPDATE, and UPDATE_STOP {@link BatchCallbackResult} * values are not supported in this operation, and will throw an * <code>UnsupportedOperationException</code> if returned by the * {@link BatchCallback} instance passed to this method.</p> * * @param criteriaBuilder the criteria builder * @param callback the callback * @param options the options * @return the number of items processed */ @SuppressWarnings("unchecked") protected Integer executeStatelessCriteriaBatchCallback(final CriteriaBuilder criteriaBuilder, final BatchCallback<T> callback, final BatchOptions options) { StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession(); Transaction tx = session.beginTransaction(); try { Criteria criteria = session.createCriteria(getType()); criteria.setFetchSize(options.getBatchSize()); criteriaBuilder.buildCriteria(criteria); ScrollableResults items = criteria.scroll(ScrollMode.FORWARD_ONLY); int count = 0; OUTER: while (items.next()) { T item = (T) items.get(0); BatchCallbackResult action = callback.handle(item); switch (action) { case DELETE: case UPDATE: case UPDATE_STOP: throw new UnsupportedOperationException("Action " + action + " not possible during " + options.getMode() + " mode batch processing"); case STOP: break OUTER; case CONTINUE: // nothing to do break; } } tx.commit(); return count; } catch (RuntimeException e) { tx.rollback(); throw e; } finally { if (session != null) { session.close(); } } }
From source file:mitm.application.djigzo.impl.hibernate.UserManagerHibernate.java
License:Open Source License
@Override public CloseableIterator<String> getEmailIterator(Integer firstResult, Integer maxResults, SortDirection sortDirection) {//from w ww .j ava2s. c om StatelessSession statelessSession = sessionManager.openStatelessSession(); Transaction tx = statelessSession.beginTransaction(); CloseableIterator<String> iterator = getStatelessDAO(statelessSession).getEmailIterator(firstResult, maxResults, sortDirection); CloseableIterator<String> commitOnCloseIterator = new CommitOnCloseIterator<String>(iterator, SessionAdapterFactory.create(statelessSession), tx); return commitOnCloseIterator; }
From source file:mitm.application.djigzo.impl.hibernate.UserManagerHibernate.java
License:Open Source License
@Override public CloseableIterator<String> searchEmail(String search, Integer firstResult, Integer maxResults, SortDirection sortDirection) {/* www. ja va 2 s .c o m*/ StatelessSession statelessSession = sessionManager.openStatelessSession(); Transaction tx = statelessSession.beginTransaction(); CloseableIterator<String> iterator = getStatelessDAO(statelessSession).searchEmail(search, firstResult, maxResults, sortDirection); CloseableIterator<String> commitOnCloseIterator = new CommitOnCloseIterator<String>(iterator, SessionAdapterFactory.create(statelessSession), tx); return commitOnCloseIterator; }
From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesCategoryManagerHibernate.java
License:Open Source License
@Override public CloseableIterator<String> getCategoryIterator() { StatelessSession statelessSession = sessionManager.openStatelessSession(); Transaction tx = statelessSession.beginTransaction(); CloseableIterator<String> iterator = getStatelessDAO(statelessSession).getCategoryIterator(); CloseableIterator<String> commitOnCloseIterator = new CommitOnCloseIterator<String>(iterator, SessionAdapterFactory.create(statelessSession), tx); return commitOnCloseIterator; }
From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesManagerHibernate.java
License:Open Source License
@Override public CloseableIterator<String> getNameIterator() { StatelessSession statelessSession = sessionManager.openStatelessSession(); Transaction tx = statelessSession.beginTransaction(); CloseableIterator<String> iterator = getStatelessDAO(statelessSession).getPreferencesIterator(category); CloseableIterator<String> commitOnCloseIterator = new CommitOnCloseIterator<String>(iterator, SessionAdapterFactory.create(statelessSession), tx); return commitOnCloseIterator; }