Skip to content

Commit e1b8c51

Browse files
First simple working transaction behavior
1 parent 28abf74 commit e1b8c51

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/SimpleEclipseStoreRepository.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public Field getIdField()
9797
public synchronized <S extends T> List<S> saveBulk(final Collection<S> entities)
9898
{
9999
final EclipseStoreTransaction transaction = this.transactionManager.getTransaction();
100+
transaction.addAction(() -> this.uncachedStore(entities));
101+
return (List<S>)entities;
102+
}
103+
104+
private synchronized <S extends T> void uncachedStore(final Collection<S> entities)
105+
{
100106
if(LOG.isDebugEnabled())
101107
{
102108
LOG.debug("Saving {} entities...", entities.size());
@@ -123,7 +129,6 @@ public synchronized <S extends T> List<S> saveBulk(final Collection<S> entities)
123129
LOG.debug("Collected {} non-entities to store.", nonEntitiesToStore.size());
124130
}
125131
this.storage.store(nonEntitiesToStore, this.domainClass, entitiesToStore);
126-
return (List<S>)entities;
127132
}
128133

129134
@Override
@@ -246,8 +251,12 @@ public void deleteById(@Nonnull final ID id)
246251
@Override
247252
public void delete(@Nonnull final T entity)
248253
{
249-
this.storage.delete(this.domainClass, this.copier.getOriginal(entity));
250-
this.copier.deregister(entity);
254+
final EclipseStoreTransaction transaction = this.transactionManager.getTransaction();
255+
transaction.addAction(() ->
256+
{
257+
this.storage.delete(this.domainClass, this.copier.getOriginal(entity));
258+
this.copier.deregister(entity);
259+
});
251260
}
252261

253262
@Override
@@ -271,7 +280,8 @@ public void deleteAll(final Iterable<? extends T> entities)
271280
@Override
272281
public void deleteAll()
273282
{
274-
this.storage.deleteAll(this.domainClass);
283+
final EclipseStoreTransaction transaction = this.transactionManager.getTransaction();
284+
transaction.addAction(() -> this.storage.deleteAll(this.domainClass));
275285
}
276286

277287
@Override

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/transactions/EclipseStoreExistingTransactionObject.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,53 @@
1515
*/
1616
package software.xdev.spring.data.eclipse.store.transactions;
1717

18+
import java.util.ArrayList;
19+
20+
import org.springframework.transaction.TransactionSystemException;
21+
22+
1823
public class EclipseStoreExistingTransactionObject implements EclipseStoreTransaction
1924
{
25+
private ArrayList<EclipseStoreTransactionAction> actions;
26+
2027
public void startTransaction()
2128
{
22-
29+
if(this.actions != null)
30+
{
31+
throw new TransactionSystemException(
32+
"Transaction is already started but it should start again. This is not allowed!");
33+
}
34+
this.actions = new ArrayList<>();
2335
}
2436

2537
public void rollbackTransaction()
2638
{
27-
39+
this.actions = null;
2840
}
2941

3042
public void commitTransaction()
3143
{
32-
44+
if(this.actions == null)
45+
{
46+
throw new TransactionSystemException(
47+
"Transaction is not started but actions should be executed. This is not allowed!");
48+
}
49+
this.actions.forEach(EclipseStoreTransactionAction::execute);
50+
this.actions = null;
3351
}
3452

3553
@Override
3654
public void addAction(final EclipseStoreTransactionAction action)
3755
{
38-
56+
if(action == null)
57+
{
58+
return;
59+
}
60+
if(this.actions == null)
61+
{
62+
throw new TransactionSystemException(
63+
"Transaction is not started but action should be added. This is not allowed!");
64+
}
65+
this.actions.add(action);
3966
}
4067
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/transactions/TransactionsTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ void accountTransaction_Working(@Autowired final PlatformTransactionManager tran
8787
this.accountRepository.findById(this.account2.getId()).get().getBalance());
8888
}
8989

90+
/**
91+
* This is actually correct (if you look at Spring Data JPA for comparison). The second change is also persisted
92+
* and
93+
* not only the first one. This seems counterintuitive, but is just like with the rest of Spring Data
94+
* Implementations.
95+
*/
9096
@Test
9197
void accountTransaction_ChangeAfterSave(@Autowired final PlatformTransactionManager transactionManager)
9298
{
@@ -101,7 +107,7 @@ void accountTransaction_ChangeAfterSave(@Autowired final PlatformTransactionMana
101107
);
102108

103109
Assertions.assertEquals(
104-
BigDecimal.valueOf(9),
110+
BigDecimal.valueOf(8),
105111
this.accountRepository.findById(this.account1.getId()).get().getBalance());
106112
}
107113

0 commit comments

Comments
 (0)