Reputation: 81
I'm launching a spring-boot application which contains an @Service
class:
@Service
@Transactional (value = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)
public class MatrixTubeService implements IMatrixTubeService {
@Autowired
MatrixTubeRepository repository;
@Override
public void flush() {
boolean txn = TransactionSynchronizationManager.isActualTransactionActive();
System.out.println("flush txn exist " + txn);
repository.flush();
}
@Override
public MatrixTube save(MatrixTube matrixTube) {
boolean txn = TransactionSynchronizationManager.isActualTransactionActive();
String txnName = TransactionSynchronizationManager.getCurrentTransactionName();
System.out.println("save txn exist " + txn + " as " + txnName);
return repository.save(matrixTube);
}
.
.
.
wrapping the calls to this simple @EnableJpaRepositories
class:
@EnableJpaRepositories(entityManagerFactoryRef = MatrixModelConfiguration.ENTITY_MANAGER_FACTORY_MATRIX,
transactionManagerRef = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)
public interface MatrixTubeRepository extends JpaRepository<MatrixTube, Long> {
}
This code is in a dependent jar and run perfect in 2 out of three parent projects. In the case where it doesn't, INSERTs/UPDATEs do not apply to the database. In fact they don't even show in the debug output.
Exception
while runningJpaRepository
implementations are working as expectedI added a call to service.flush()
directly after a service.save()
of a newly created object and again after a few updates a previously read object (within the same txn) and in both instances, I see the txn is active in the flush method, but when repository.flush()
is called, I get a "no transaction is in progress" exception.
What am I missing?
Upvotes: 3
Views: 14986
Reputation: 31
Maybe spring-test set rollback default value is true,
adding @Rollback(false)
soveld my problem.
Upvotes: 3
Reputation: 81
The problem had to do with spawning a new thread from within the transaction, which actually performed the SQL inserts well after (in computer time) the transaction had been committed.
Not sure why there were no exceptions thrown, but getting the inserts back into the transactional thread fixed everything.
Upvotes: 0
Reputation: 10938
With springdatajpa you do not need all this magic. txn etc.
Use this annotation in you main application class.
@EnableJpaRepositories(entityManagerFactoryRef = MatrixModelConfiguration.ENTITY_MANAGER_FACTORY_MATRIX,
transactionManagerRef = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)
Start with this tutorial. Spring data jpa is a little magical comparing to standard Hibernate/jpa.
https://spring.io/guides/gs/accessing-data-jpa/
Upvotes: 1