Spring @Transactional 注释不起作用
Spring @Transactional annotation is not working
我正在尝试 运行 entityManager.merge(myEntity) 在以下方法中,但似乎忽略了 @Transactional 注释。 Hibernate 配置似乎没问题,因为我可以成功地从数据库中获取数据,但无法写入数据库。我正在使用 Spring 版本 3.2.3。为什么写入数据库操作不起作用?
我的方法不行
package com.reflections.importer.bls;
...
@Service
class BlsGovImporter {
...
@Transactional
private void importSeries(String externalId) {
// This works. The dao is using EntityManager too
Series series = seriesDao.findByExternalId(externalId);
series.getValues().addAll(fetchNewValues());
// This does not work and no exception is thrown
entityManager.merge(series);
}
因为是用在私有方法上。 Spring Docs:
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation
only to methods with public visibility. If you do annotate protected,
private or package-visible methods with the @Transactional annotation,
no error is raised, but the annotated method does not exhibit the
configured transactional settings. Consider the use of AspectJ (see
below) if you need to annotate non-public methods.
当它是私有的时,显然是从同一个 class 中调用的。但是 Spring 调用需要通过代理才能使其正常工作。因此需要从另一个 bean 调用该方法。
其他选项是用@Transactional 注释class。
luboskrnac 实际上为我准确地回答了它,但只是将其添加到 Spring 可能对是否使用代理感到困惑的新手。
请参阅此页面解释即使您在同一个 class 中调用 @Transactional 方法的情况,因为您在不相同 class 的情况下调用它,它不会被调用代理。
我正在尝试 运行 entityManager.merge(myEntity) 在以下方法中,但似乎忽略了 @Transactional 注释。 Hibernate 配置似乎没问题,因为我可以成功地从数据库中获取数据,但无法写入数据库。我正在使用 Spring 版本 3.2.3。为什么写入数据库操作不起作用?
我的方法不行
package com.reflections.importer.bls;
...
@Service
class BlsGovImporter {
...
@Transactional
private void importSeries(String externalId) {
// This works. The dao is using EntityManager too
Series series = seriesDao.findByExternalId(externalId);
series.getValues().addAll(fetchNewValues());
// This does not work and no exception is thrown
entityManager.merge(series);
}
因为是用在私有方法上。 Spring Docs:
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
当它是私有的时,显然是从同一个 class 中调用的。但是 Spring 调用需要通过代理才能使其正常工作。因此需要从另一个 bean 调用该方法。
其他选项是用@Transactional 注释class。
luboskrnac 实际上为我准确地回答了它,但只是将其添加到 Spring 可能对是否使用代理感到困惑的新手。
请参阅此页面解释即使您在同一个 class 中调用 @Transactional 方法的情况,因为您在不相同 class 的情况下调用它,它不会被调用代理。