List of usage examples for org.springframework.transaction.support TransactionTemplate TransactionTemplate
public TransactionTemplate(PlatformTransactionManager transactionManager)
From source file:org.projectforge.framework.persistence.xstream.HibernateXmlConverter.java
/** * Schreibt alle Objekte der Datenbank in den angegebenen Writer.<br/> * <b>Warnung!</b> Bei der Serialisierung von Collections wird derzeit nur {@link java.util.Set} sauber untersttzt. * /*from w ww. j a v a 2 s .c o m*/ * @param writer Ziel fr die XML-Datei. * @param includeHistory bei false werden die History Eintrge nicht geschrieben * @param preserveIds If true, the object ids will be preserved, otherwise new ids will be assigned through xstream. */ public void dumpDatabaseToXml(final Writer writer, final boolean includeHistory, final boolean preserveIds) { final TransactionTemplate tx = new TransactionTemplate( new HibernateTransactionManager(hibernate.getSessionFactory())); tx.execute(new TransactionCallback() { @Override public Object doInTransaction(final TransactionStatus status) { hibernate.execute(new HibernateCallback() { @Override public Object doInHibernate(final Session session) throws HibernateException { writeObjects(writer, includeHistory, session, preserveIds); status.setRollbackOnly(); return null; } }); return null; } }); }
From source file:org.sakaiproject.component.app.help.HelpManagerImpl.java
private void dropExistingContent() { if (LOG.isDebugEnabled()) { LOG.debug("dropExistingContent()"); }// w w w .ja v a2 s. c o m TransactionTemplate tt = new TransactionTemplate(txManager); tt.execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { getHibernateTemplate().bulkUpdate("delete CategoryBean"); getHibernateTemplate().flush(); return null; } }); }
From source file:org.sakaiproject.portal.service.BullhornServiceImpl.java
public void update(Observable o, final Object arg) { if (arg instanceof Event) { Event e = (Event) arg; String event = e.getEvent(); // We add this comparation with UNKNOWN_USER because implementation of BaseEventTrackingService // UNKNOWN_USER is an user in a server without session. if (HANDLED_EVENTS.contains(event) && !EventTrackingService.UNKNOWN_USER.equals(e.getUserId())) { String ref = e.getResource(); String context = e.getContext(); String[] pathParts = ref.split("/"); String from = e.getUserId(); long at = e.getEventTime().getTime(); try { BullhornHandler handler = handlerMap.get(event); if (handler != null) { Optional<List<BullhornData>> result = handler.handleEvent(e, countCache); if (result.isPresent()) { result.get().forEach(bd -> { if (bd.isSocial()) { doSocialInsert(bd.getFrom(), bd.getTo(), event, ref, e.getEventTime(), bd.getUrl()); } else { doAcademicInsert(from, bd.getTo(), event, ref, bd.getTitle(), bd.getSiteId(), e.getEventTime(), bd.getUrl()); }// w w w . j a v a 2 s.c om }); } } else if (LessonBuilderEvents.COMMENT_CREATE.equals(event)) { try { long commentId = Long.parseLong(pathParts[pathParts.length - 1]); SimplePageComment comment = simplePageToolDao.findCommentById(commentId); String url = simplePageToolDao.getPageUrl(comment.getPageId()); if (url != null) { List<String> done = new ArrayList<>(); // Alert tutor types. List<User> receivers = securityService.unlockUsers( SimplePage.PERMISSION_LESSONBUILDER_UPDATE, "/site/" + context); for (User receiver : receivers) { String to = receiver.getId(); if (!to.equals(from)) { doAcademicInsert(from, to, event, ref, "title", context, e.getEventTime(), url); done.add(to); countCache.remove(to); } } // Get all the comments in the same item List<SimplePageComment> comments = simplePageToolDao .findCommentsOnItems(Arrays.asList(new Long[] { comment.getItemId() })); if (comments.size() > 1) { // Not the first, alert all the other commenters unless they already have been for (SimplePageComment c : comments) { String to = c.getAuthor(); if (!to.equals(from) && !done.contains(to)) { doAcademicInsert(from, to, event, ref, "title", context, e.getEventTime(), url); done.add(to); countCache.remove(to); } } } } else { log.error("null url for page {}", comment.getPageId()); } } catch (NumberFormatException nfe) { log.error("Caught number format exception whilst handling events", nfe); } } else if (SiteService.EVENT_SITE_PUBLISH.equals(event)) { final String siteId = pathParts[2]; TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { final List<BullhornAlert> deferredAlerts = sessionFactory.getCurrentSession() .createCriteria(BullhornAlert.class).add(Restrictions.eq("deferred", true)) .add(Restrictions.eq("siteId", siteId)).list(); for (BullhornAlert da : deferredAlerts) { da.setDeferred(false); sessionFactory.getCurrentSession().update(da); countCache.remove(da.getToUser()); } } }); } } catch (Exception ex) { log.error("Caught exception whilst handling events", ex); } } } }
From source file:org.sakaiproject.portal.service.BullhornServiceImpl.java
private void doAcademicInsert(String from, String to, String event, String ref, String title, String siteId, Date eventDate, String url) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { BullhornAlert ba = new BullhornAlert(); ba.setAlertType(ACADEMIC);/*from w w w. ja va 2s .co m*/ ba.setFromUser(from); ba.setToUser(to); ba.setEvent(event); ba.setRef(ref); ba.setTitle(title); ba.setSiteId(siteId); ba.setEventDate(eventDate.toInstant()); ba.setUrl(url); try { ba.setDeferred(!siteService.getSite(siteId).isPublished()); } catch (IdUnusedException iue) { log.warn("Failed to find site with id {} while setting deferred to published", siteId); } sessionFactory.getCurrentSession().persist(ba); } }); }
From source file:org.sakaiproject.portal.service.BullhornServiceImpl.java
private void doSocialInsert(String from, String to, String event, String ref, Date eventDate, String url) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { BullhornAlert ba = new BullhornAlert(); ba.setAlertType(SOCIAL);// ww w . java 2 s . com ba.setFromUser(from); ba.setToUser(to); ba.setEvent(event); ba.setRef(ref); ba.setTitle(""); ba.setSiteId(""); ba.setEventDate(eventDate.toInstant()); ba.setUrl(url); ba.setDeferred(false); sessionFactory.getCurrentSession().persist(ba); } }); }
From source file:org.sipfoundry.sipxconfig.bulk.RowInserter.java
public final void execute(Object input) { T row = (T) input;/*from w w w . j a v a 2 s . c o m*/ String jobDescription = dataToString(row); final Serializable jobId = m_jobContext.schedule("Import data: " + jobDescription); TransactionTemplate tt = new TransactionTemplate(m_transactionManager); try { TransactionCallback callback = new JobTransaction(jobId, row); tt.execute(callback); } catch (UserException e) { // ignore user exceptions - just log them m_jobContext.failure(jobId, null, e); } catch (RuntimeException e) { // log and rethrow other exceptions m_jobContext.failure(jobId, null, e); throw e; } }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testReceiveInExternalTransaction() throws Exception { template.convertAndSend(ROUTE, "message"); template.setChannelTransacted(true); String result = new TransactionTemplate(new TestTransactionManager()) .execute(new TransactionCallback<String>() { public String doInTransaction(TransactionStatus status) { return (String) template.receiveAndConvert(ROUTE); }//from ww w . j av a 2s.co m }); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testReceiveInExternalTransactionAutoAck() throws Exception { template.convertAndSend(ROUTE, "message"); // Should just result in auto-ack (not synched with external tx) template.setChannelTransacted(true); String result = new TransactionTemplate(new TestTransactionManager()) .execute(new TransactionCallback<String>() { public String doInTransaction(TransactionStatus status) { return (String) template.receiveAndConvert(ROUTE); }// w w w.ja v a 2 s . c o m }); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testReceiveInExternalTransactionWithRollback() throws Exception { // Makes receive (and send in principle) transactional template.setChannelTransacted(true); template.convertAndSend(ROUTE, "message"); try {//from w w w . j ava2s.c o m new TransactionTemplate(new TestTransactionManager()).execute(new TransactionCallback<String>() { public String doInTransaction(TransactionStatus status) { template.receiveAndConvert(ROUTE); throw new PlannedException(); } }); fail("Expected PlannedException"); } catch (PlannedException e) { // Expected } String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testReceiveInExternalTransactionWithNoRollback() throws Exception { // Makes receive non-transactional template.setChannelTransacted(false); template.convertAndSend(ROUTE, "message"); try {//from w ww . j av a 2 s.c o m new TransactionTemplate(new TestTransactionManager()).execute(new TransactionCallback<String>() { public String doInTransaction(TransactionStatus status) { template.receiveAndConvert(ROUTE); throw new PlannedException(); } }); fail("Expected PlannedException"); } catch (PlannedException e) { // Expected } // No rollback String result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }