JTA EntityManager不能使用getTransaction()-只能在控制器中使用

时间:2018-10-08 15:19:59

标签: spring-boot spring-data-jpa jta

我正在尝试使用Spring Data JPA存储库来获取和保留实体。当我从例如内部使用此存储库时石英工作,一切正常。当我尝试从RestController中使用此存储库时,收到以下错误消息:“ JTA EntityManager无法使用getTransaction()”。当然,我用谷歌搜索了一个解决方案,但是看来我的案子与我发现的案子有些不同。有什么想法可能导致这种错误吗?

使用此存储库的Service类示例:

@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
    }
  }
...

我的回购代码:

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);
}

控制器代码:

@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();
  }
...

这是我配置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;
  }
...

1 个答案:

答案 0 :(得分:0)

对不起,我花了一段时间回答。我发现了问题。 https://stackoverflow.com/users/1126526/manish确实提出了这个问题:我不得不将dataSource更改为jtaDataSource。我不知道,为什么我以前没看到过。