交易性
默认情况下,方法继承自原油仓库继承了从中的事务配置SimpleJpaRepository.
对于读取作,事务配置只读旗帜设置为true.
其他所有配置均为平面@Transactional所以默认的交易配置是适用的。
由事务仓库片段支持的存储库方法继承了实际片段方法的事务属性。
如果你需要调整仓库中某个方法的事务配置,请在仓库界面中重新声明该方法,如下:
public interface UserRepository extends CrudRepository<User, Long> {
@Override
@Transactional(timeout = 10)
public List<User> findAll();
// Further query method declarations
}
这样做会导致findAll()以10秒超时且不使用只读旗。
改变事务行为的另一种方法是使用(通常)覆盖多个存储库的表象或服务实现。其目的是为非CRUD作定义事务边界。以下示例展示了如何将此类门面用于多个存储库:
@Service
public class UserManagementImpl implements UserManagement {
private final UserRepository userRepository;
private final RoleRepository roleRepository;
public UserManagementImpl(UserRepository userRepository,
RoleRepository roleRepository) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
}
@Transactional
public void addRoleToAllUsers(String roleName) {
Role role = roleRepository.findByName(roleName);
for (User user : userRepository.findAll()) {
user.addRole(role);
userRepository.save(user);
}
}
}
该示例导致调用addRoleToAllUsers(...)在事务中运行(参与现有事务,或如果没有交易在运行时创建新的事务)。仓库中的事务配置被忽略,因为外部事务配置决定了实际使用的事务配置。注意你必须激活<tx:注释驱动 />或使用@EnableTransactionManagement明确用于使基于注释的界面配置正常工作。
这个例子假设你使用了分量扫描。
注意 调用救从 JPA 的角度来看,这并非绝对必要,但应当仍然存在,以保持与 Spring Data 提供的仓库抽象的一致性。
事务查询方法
声明查询方法(包括默认方法)默认不会应用任何事务配置。
要以事务方式运行这些方法,请使用@Transactional在你定义的仓库接口中,如下例所示:
@Transactional(readOnly = true)
interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastname(String lastname);
@Modifying
@Transactional
@Query("delete from User u where u.active = false")
void deleteInactiveUsers();
}
通常,你想要的只读将 设置为 的标志true,因为大多数查询方法只读取数据。与此形成对比的是,deleteInactiveUsers()利用@Modifying注释并覆盖事务配置。因此,该方法运行于只读旗帜设置为false.
|
你可以用事务进行只读查询,并通过设置 |
|
同时讨论示例 |