List of usage examples for org.springframework.transaction.support TransactionCallback TransactionCallback
TransactionCallback
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 w w . j a v a2 s. co 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.projectforge.user.UserTest.java
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test// w w w .java 2s.c o m public void history() { logon(TEST_ADMIN_USER); txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { initTestDB.addUser("UserTest.historyUser1a"); initTestDB.addUser("UserTest.historyUser1b"); initTestDB.addUser("UserTest.historyUser2a"); initTestDB.addUser("UserTest.historyUser2b"); initTestDB.addGroup("UserTest.historyGroup1", new String[] { "UserTest.historyUser1a", "UserTest.historyUser1b" }); initTestDB.addGroup("UserTest.historyGroup2", (String[]) null); initTestDB.addGroup("UserTest.historyGroup3", (String[]) null); return null; } }); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { // Checking history entries of user for new group: HistoryEntry[] historyEntries = userDao.getHistoryEntries(getUser("UserTest.historyUser1a")); assertEquals(2, historyEntries.length); // insert and update assignedGroups final HistoryEntry entry = historyEntries[0]; // Update assignedGroups entry assertEquals(1, entry.getDelta().size()); assertEquals("", entry.getDelta().get(0).getOldValue()); assertGroupIds(new String[] { "UserTest.historyGroup1" }, entry.getDelta().get(0).getNewValue()); // Checking history entries of new group: historyEntries = groupDao.getHistoryEntries(getGroup("UserTest.historyGroup1")); assertEquals(1, historyEntries.length); // insert return null; } }); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { // (Un)assigning groups: final PFUserDO user = userDao.internalGetById(getUserId("UserTest.historyUser1a")); final Set<GroupDO> groupsToAssign = new HashSet<GroupDO>(); groupsToAssign.add(getGroup("UserTest.historyGroup2")); groupsToAssign.add(getGroup("UserTest.historyGroup3")); final Set<GroupDO> groupsToUnassign = new HashSet<GroupDO>(); groupsToUnassign.add(getGroup("UserTest.historyGroup1")); groupDao.assignGroups(user, groupsToAssign, groupsToUnassign); return null; } }); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { // Checking history of updated user: HistoryEntry[] historyEntries = userDao.getHistoryEntries(getUser("UserTest.historyUser1a")); assertEquals(3, historyEntries.length); assertUserHistoryEntry(historyEntries[0], new String[] { "UserTest.historyGroup2", "UserTest.historyGroup3" }, new String[] { "UserTest.historyGroup1" }); // Checking history entries of updated groups: historyEntries = groupDao.getHistoryEntries(getGroup("UserTest.historyGroup1")); final GroupDO group = groupDao.internalGetById(getGroupId("UserTest.historyGroup1")); final Set<PFUserDO> users = group.getAssignedUsers(); assertEquals(1, users.size()); // Assigned users are: "UserTest.historyUser1b" assertEquals("2 history entries (1 insert and 1 assigned users", 2, historyEntries.length); // insert and update assignedUsers assertGroupHistoryEntry(historyEntries[0], null, new String[] { "UserTest.historyUser1a" }); return null; } }); }
From source file:org.projectforge.user.UserTest.java
@Test public void testUniqueUsernameDO() { final Serializable[] ids = new Integer[2]; txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { PFUserDO user = createTestUser("42"); ids[0] = userDao.internalSave(user); user = createTestUser("100"); ids[1] = userDao.internalSave(user); return null; }//from w ww . j a va2s . c o m }); txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { final PFUserDO user = createTestUser("42"); assertTrue("Username should already exist.", userDao.doesUsernameAlreadyExist(user)); user.setUsername("5"); assertFalse("Signature should not exist.", userDao.doesUsernameAlreadyExist(user)); userDao.internalSave(user); return null; } }); txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { final PFUserDO dbBook = userDao.internalGetById(ids[1]); final PFUserDO user = new PFUserDO(); user.copyValuesFrom(dbBook); assertFalse("Signature does not exist.", userDao.doesUsernameAlreadyExist(user)); user.setUsername("42"); assertTrue("Signature does already exist.", userDao.doesUsernameAlreadyExist(user)); user.setUsername("4711"); assertFalse("Signature does not exist.", userDao.doesUsernameAlreadyExist(user)); userDao.internalUpdate(user); return null; } }); txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { final PFUserDO user = userDao.internalGetById(ids[1]); assertFalse("Signature does not exist.", userDao.doesUsernameAlreadyExist(user)); return null; } }); }
From source file:org.sakaiproject.component.app.help.HelpManagerImpl.java
private void dropExistingContent() { if (LOG.isDebugEnabled()) { LOG.debug("dropExistingContent()"); }//from w w w . j av a 2s. co 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.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); }/* www . j a v a 2 s .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 . j ava2 s .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 testReceiveInExternalTransactionWithRollback() throws Exception { // Makes receive (and send in principle) transactional template.setChannelTransacted(true); template.convertAndSend(ROUTE, "message"); try {/* w ww . j a va2s . co 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 {/* w w w. j a v 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); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testSendInExternalTransaction() throws Exception { template.setChannelTransacted(true); new TransactionTemplate(new TestTransactionManager()).execute(new TransactionCallback<Void>() { public Void doInTransaction(TransactionStatus status) { template.convertAndSend(ROUTE, "message"); return null; }//w w w. j a v a 2s .com }); 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 testSendInExternalTransactionWithRollback() throws Exception { template.setChannelTransacted(true); try {/*from ww w . j a va2s . c o m*/ new TransactionTemplate(new TestTransactionManager()).execute(new TransactionCallback<Void>() { public Void doInTransaction(TransactionStatus status) { template.convertAndSend(ROUTE, "message"); throw new PlannedException(); } }); fail("Expected PlannedException"); } catch (PlannedException e) { // Expected } String result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }