问题出在@Transactional中,在我的配置中spring应用程序没有使用它。我怎么能修好它?
...REST控制器没有任何事务性方法,它只使用specifiedServices加载实体。依赖集合(如果未加载到服务中)应为空。
应用程序启动程序类:
@SpringBootApplication
@EntityScan("com.vl.pmanager.domain.model")
@EnableJpaRepositories("com.vl.pmanager.domain.repository")
@EnableTransactionManagement
public class ProjectManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectManagerApplication.class, args);
}
}
@EntityScan("com.vl.pmanager.domain.model")
@EnableJpaRepositories("com.vl.pmanager.domain.repository")
我还尝试将@Transactional添加到存储库接口中,但对我来说并不起作用
package com.vl.pmanager.domain.repository;
import com.vl.pmanager.domain.model.Tag;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
}
所以我从存储库中删除了@Transactional,创建了其他服务层来使用注释管理它,并将服务注入到Controller:
package com.vl.pmanager.domain.db.service.api;
import com.vl.pmanager.domain.model.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface ITagManager {
Page<Tag> findAll(Pageable pageable);
Tag findOne(Long id);
}
// ======================== TAG SERVICE WRAPPED WITH TRANSACTION ==========================
package com.vl.pmanager.domain.db.service;
import com.vl.pmanager.domain.db.service.api.ITagManager;
import com.vl.pmanager.domain.model.Tag;
import com.vl.pmanager.domain.repository.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class TagManager implements ITagManager {
@Autowired
private TagRepository tagRepository;
@Override
public Page<Tag> findAll(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag findOne(Long id) {
return tagRepository.findOne(id);
}
}
// ====================== REST CONTROLLER ============================
package com.vl.pmanager.web;
import com.vl.pmanager.domain.db.service.api.ITagManager;
import com.vl.pmanager.domain.model.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/tags")
public class TagController {
private static final int DEFAULT_PAGE_SIZE = 50;
@Autowired
private ITagManager tagManager;
@RequestMapping(method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public Page<Tag> getTags(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable) {
return tagManager.findAll(pageable);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public Tag getTag(@PathVariable("id") Long id) {
return tagManager.findOne(id);
}
}
标记实体有一个字段集projectInfo映射到具有@ManyTomany关系的其他实体,该实体具有FetchType fetch()default lazy;因此返回结果不能包含依赖实体,但它包含依赖实体。
我还监视了DB日志:
// make request - load data using db service to controller level
Hibernate: select count(tag0_.id) as col_0_0_ from tag tag0_
Hibernate: select tag0_.id as id1_6_, tag0_.description as descript2_6_, tag0_.name as name3_6_ from tag tag0_ limit ?
// converting data to JSON automatically
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
而且我知道只有5个额外的请求,因为我只有5个依赖于标记PROJECT_INFO。因此,作为结论,我的事务级别不管理事务。...我已经检查了datasource和transactionManager注入beans-创建了一个。启动时或运行时无错误、无警告...
@jbnizet
这不是交易问题。在文档中查找spring.jpa.open-in-view属性,如果不想要它,则将其设置为false
我已经将这个答案添加到标记问题中,就像关闭一样。如果我关闭了jpa,open in view事务就会像我预期的那样工作。
本文向大家介绍详解SpringBoot的事务管理,包括了详解SpringBoot的事务管理的使用技巧和注意事项,需要的朋友参考一下 Springboot内部提供的事务管理器是根据autoconfigure来进行决定的。 比如当使用jpa的时候,也就是pom中加入了spring-boot-starter-data-jpa这个starter之后。 Springboot会构造一个JpaTransacti
事务处理(transaction processing) 可以用来维护数据的完整性,保证SQL的操作要么完全执行,要么完全不执行,如果发生错误就进行撤销。 保证数据的完整性。 保证数据不受外影响。 事务处理的几道术语 事务(transaction) 一组SQL语句 退回(rollback)撤销执行SQL语句的过程 提交(commit) 将为执行的SQL语句写入数据库表 保留点(savepoint)
17. 事务管理
数据库的事务就是将任意多个SQL语句看作一个整体,只有这些SQL语句都成功执行,DBMS才会保存这些SQL语句对数据库的修改(事务提交)。否则,数据库将恢复到执行SQL语句之前的状态(事务回滚)。大多数DBMS都支持两种事务模式:隐式模式和显式模式。当执行每一条SQL语句时,无需进行事务提交,就可以直接将修改结果保存到数据库中。这叫做隐式事务模式。显式模式必须使用相应的语句或命令开起事务、提交事务
主要内容:一、事务(Transaction),二、MySql中的应用,三、源码分析,四、总结一、事务(Transaction) 事务是什么?按照书上说的就是系统的一套操作为了保持数据的完整性必须符合ACID的特性,即原子性(Atomic)、一致性(Consistency)、隔离性(Isolation)、 持久性(Durability)。原子性比较好理解,操作要么全执行完成,要么全不执行完,实现这种方式就要支持回滚操作。而一致性指的是事务在改变状态时,要保证所有的访问得到的结果是相同的。一
我正在尝试获得骆驼路线JMS- 下面的例子说明了如果REST服务的服务器出现故障而无法交付route时会发生什么情况。 我得到了正确的例外: 但是消息被消费并从队列中删除。我的假设是使用事务/事务骆驼和AMQ可以解决这个问题并将消息移动到ActiveMQ.DLQ. 我已经阅读了《骆驼行动》第一版的第9章,并在谷歌上搜索,但没有找到任何解决我问题的方法。 我知道我可以创建/定义自己的Transact