spring-transactions

Why won't the transaction start in my junit test cases?

扶醉桌前 提交于 2019-12-08 08:17:02
问题 I have a Spring 3.1 MVC + Hibernate 3.6 project with its junit4 test suit. My problem is that there is no transaction starting in my test cases, even thought I added a @Transactional . My test case calls a controller and a dao. In the controller, a transaction is started anyway, so it does not complain. In the dao, I added a @Transactional(propagation = Propagation.MANDATORY) to be sure it will take the test's transaction. And currently it raises an IllegalTransactionStateException , which I

Why multiple @ComponentScan annotations corrupt my AspectJ transaction?

夙愿已清 提交于 2019-12-08 06:09:40
问题 I'm asking this question in continue to the issue I described here. I got some unexpected behavior when I used AspectJ transactions - some operations were immediately committed to the DB, before that transaction was ended. And therefore they were not rolled back in case of error. When I changed the Advice Mode ( @EnableTransactionManagement(mode=AdviceMode.ASPECTJ) ) to PROXY all was working fine. I found out that I had multiple classes annotated with the @ComponentScan annotation, and when I

Issue with @Transactional annotations in Spring JPA

試著忘記壹切 提交于 2019-12-08 04:52:44
问题 I have a doubt related to transactions within transactions. For background, I have a School entity object which has Set of Students entity object mapped to it. I am using Spring Data JPA which is taking care of all the crud operations. I have a SchoolManagementService class which has @Transactional(readonly=true) set at the class level and for all updating methods I am using @Transactional over them. In my SchoolManagementService class I have a method deleteStudents(List) which I have marked

spring boot transaction manager advice to roll back all exceptions

强颜欢笑 提交于 2019-12-08 03:25:55
问题 Prior to Spring Boot, one was able to globally say (without having to identify on each @Transactional element) that all checked exceptions should roll back a transaction: <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception" timeout="20"/> </tx:attributes> </tx:advice> How is the above achieved via Java config in the Spring Boot world? 回答1: Use custom annotations If you find you are repeatedly using the same attributes

Spring boot @Transactional

送分小仙女□ 提交于 2019-12-07 13:18:54
问题 Does spring boot automatically add @Transactional annotation at controller layer? I tried putting @Transactional at service layer but it seems that the controller layer is overriding the annotation. I have this config <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" isolation="READ_COMMITTED" propagation="NOT_SUPPORTED" /> <tx:method name="load*" read-only="true" isolation="READ_COMMITTED" propagation="NOT_SUPPORTED" /

@Transactional in bidirectional relation with Spring Data returns null

与世无争的帅哥 提交于 2019-12-07 08:38:47
问题 I am using Spring Data and @Transactional annotation(for automatic rollback after tests). I have simple bidirectional relation between account and user(owning side): @Entity @Table(name = "ACCOUNT_T") public class AccountEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String email; private String password; private String verificationCode; private Boolean active = false; @OneToOne(mappedBy = "account", fetch = FetchType.EAGER, cascade = {CascadeType

spring boot transaction manager advice to roll back all exceptions

只愿长相守 提交于 2019-12-07 03:42:28
Prior to Spring Boot, one was able to globally say (without having to identify on each @Transactional element) that all checked exceptions should roll back a transaction: <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception" timeout="20"/> </tx:attributes> </tx:advice> How is the above achieved via Java config in the Spring Boot world? Use custom annotations If you find you are repeatedly using the same attributes with @Transactional on many different methods, then Spring's meta-annotation support allows you to define

Spring transactions - Mixing @Transactional with <tx:advice> into a custom annotation

与世无争的帅哥 提交于 2019-12-06 21:35:36
My goal is to have some way of declaring my service classes as transactional. I dont want to leave it as an explicit declaration in spring configuration. Many times in past we have created new services and forgot to declare transactions around them. Hence my intention is that if i have something like @TransactionalService custom annotation, it should do the following :- 1. provides transactional support 2. declares some default rules for transactional support as spring currently provides as shown below. But unlike spring, i would like the below to be part of my @TransactionService annotation.

Hibernate transaction manager configurations in Spring

心已入冬 提交于 2019-12-06 19:27:03
问题 In my project I use Hibernate with programmatic transaction demarcation. Every time in my Service methods i write something similar to this. Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); .. perform operation here session.getTransaction().commit(); Now i'm going to refactor my code with declarative transaction management. What i got now ... <context:component-scan base-package="package.*"/> <bean id="mySessionFactory" class="org.springframework

Manage transactions with multiple datasource, entity managers for same application code

梦想与她 提交于 2019-12-06 16:39:28
I'm building a spring boot application that has multiple datasources, entity managers, transaction managers and databases. Each one is for a customer and share same DAOs, Services. The swicth betweeen datasource work perfectly. But I have problem with transaction Here my configuration: package org.foo.config; @Configuration @EnableJpaRepositories(basePackages = "org.foo") @EnableTransactionManagement public class DataSourceConfiguration { @Value("#{'${load.datasources}'.split(',')}") private List<String> toLoadDatasources; @Value("${default.datasource}") private String defaultDatasource; @Bean