I am trying to use a Spring Data JPA repository to get and persist entities. When I use this repository from within a e.g. a Quartz job, everything works perfectly. When I try to use this repo from within a RestController, I get the following error message: "A JTA EntityManager cannot use getTransaction()". Of course I googled for a solution, but it seems that my case is a bit different than the cases that I have found. Any ideas what might cause this kind of error?
Example of Service class using this repo:
@Service
public class ServiceToTablesMappingServiceImpl implements ServiceToTablesMappingService {
@Autowired
private ServiceToTablesMappingRepository repo;
/**
* {@inheritDoc}
*/
@Override
public void createMapping(String serviceName, Set<String> tables) {
for (String table : tables) {
ServiceToTablesMappingEntity entity = new ServiceToTablesMappingEntity();
entity.setServiceName(serviceName);
entity.setTableName(table);
this.repo.save(entity); //this line causes the error
}
}
...
Code of my repo:
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import xxx.ServiceToTablesMappingEntity;
@Repository
public interface ServiceToTablesMappingRepository extends CrudRepository<ServiceToTablesMappingEntity, Long> {
@Transactional
List<ServiceToTablesMappingEntity> findByServiceName(String serviceName);
}
Code of Controller:
@RestController
public class InitialLoadController {
...
@Autowired
private ServiceToTablesMappingService regService;
/**
* Triggers the initial load workflow preparation
*
* @param service The service which tries to prepare
* @param request The request which contains a list of tables along with some other information about the subscriber
* @return Return the 200 status code if everything went fine
*/
@PostMapping("/init-prepare")
public ResponseEntity<Void> initPrepare(@RequestBody InitialLoadRequestDto request) {
// create mapping
Set<String> tables = request.getTables();
String service = request.getServiceName();
this.regService.createMapping(service, tables);
...
return ResponseEntity.ok().build();
}
...
And here´s how I configure the entityManager:
@Configuration
@DependsOn("transactionManager")
@ComponentScan({ "xxx", "yyy" })
@EnableJpaRepositories(basePackages = { "xxx",
"yyy" }, entityManagerFactoryRef = "datasupplyEntityManager", transactionManagerRef = "transactionManager", queryLookupStrategy = Key.CREATE_IF_NOT_FOUND)
@EnableConfigurationProperties(DbDatasourceProperties.class)
public class DatasupplyConfig {
...
@Bean(name = "datasupplyEntityManager")
@DependsOn("jpaVendorAdapter")
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(JpaVendorAdapter jpaVendorAdapter,
DataSource xaDataSource) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
properties.put("javax.persistence.transactionType", "JTA");
properties.put("hibernate.temp.use_jdbc_metadata_defaults", this.useJdbcMetadataDefaults);
LocalContainerEntityManagerFactoryBean entityManagerBean = new LocalContainerEntityManagerFactoryBean();
entityManagerBean.setDataSource(xaDataSource);
entityManagerBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerBean.setPackagesToScan("xxx",
"yyyy");
entityManagerBean.setJpaPropertyMap(properties);
return entityManagerBean;
}
...
Sorry, it took me a while to answer. I have found the issue. The issue was indeed what https://stackoverflow.com/users/1126526/manish suggested: I had to change the dataSource to a jtaDataSource. I don´t know, why I didn´t see that before.