Spring-Data JPA: InvalidDataAccessApiUsageException для @TransactionalJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Spring-Data JPA: InvalidDataAccessApiUsageException для @Transactional

Сообщение Anonymous »

Я запускаю приложение Spring Boot с @EnableTransactionManagement и хочу использовать @Transactional(readOnly = true) для некоторых запросов к базе данных.

Но я получаю непонятное сообщение об ошибке.
Я использую Spring, Spring Boot и Spring Data JPA.

MySpringBootApplication.java

Код: Выделить всё

@SpringBootApplication
@EnableTransactionManagement
@ComponentScan("com.deutscheboerse.regrephub")
@EntityScan(basePackages = "com.deutscheboerse.regrephub")
@EnableJpaRepositories(basePackages = "com.deutscheboerse.regrephub")
@Slf4j
public class MySpringBootApplication
{
... Some @Autowired variables ...

public static void main(String[] args)
{
SpringApplication.run(MySpringBootApplication.class, args);
}

...
}
MySpringBootApplicationConfiguration.java

Код: Выделить всё

@Configuration
@EnableEncryptableProperties
@EnableTransactionManagement
@EnableAsync
@Slf4j
public class MySpringBootApplicationConfiguration
{
... Some @Autowired variables ...

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource()
{
return DataSourceBuilder
.create(this.dataSourceProperties.getClassLoader())
.url(this.dataSourceProperties.getUrl())
.username(this.dataSourceProperties.getUsername())
.password(this.dataSourceProperties.getPassword())
.build();
}

...
}
MyBeanDao.java

Код: Выделить всё

@Repository
public interface MyBeanDao extends JpaRepository
{
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE))
@Query(value = "SELECT * FROM MY_TABLE", nativeQuery = true)
@Transactional(readOnly = true)
Stream streamAll();
}
MyBeanService.java

Код: Выделить всё

@Service
@Slf4j
public class MyBeanService extends AbstractService
{
@Autowired
public MyBeanService(...)
{
...
}

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public void handleRequest(Object request, Message msg)
{
try (Stream data = myBeanDao.streamAll())
{
...
}
catch (Exception e)
{
...
}
}
}
Когда я запускаю SpringBootApplication, я получаю следующие сообщения/ошибки журнала:

Код: Выделить всё

[TransactionInterceptor:474] Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.streamAll]
[TransactionInterceptor:517] Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.streamAll] after exception: org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
[RuleBasedTransactionAttribute:131] Applying rules to determine whether transaction should rollback on org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
[RuleBasedTransactionAttribute:148] Winning rollback rule is: null
[RuleBasedTransactionAttribute:153] No relevant rollback rule found: applying default rules
Сначала JPA открывает транзакцию и немедленно закрывает ее с исключением, которое я хочу выполнить методом потокового запроса без окружающей транзакции.
Было ли у кого-нибудь такое раньше?!

Подробнее здесь: https://stackoverflow.com/questions/483 ... nsactional
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»