相关疑难解决方法(0)

Spring @Transactional注释不起作用

我正在尝试在以下方法中运行entityManager.merge(myEntity),但似乎忽略了@Transactional注释.Hibernate配置似乎很好,因为我可以成功地从数据库中获取数据,但是无法写入数据库.我使用的是Spring 3.2.3版.为什么编写db操作不起作用?

我的方法不起作用

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);
    }
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-mvc spring-transactions

2
推荐指数
1
解决办法
3013
查看次数

Spring Boot中的同步事务

在Springboot中,我称之为服务的每个服务都会打开一个事务,当服务返回时它将关闭该连接,但就我而言,我需要创建一个将同步运行的方法(该方法仅在非同步方法中运行),并且他需要无论是否打开了一个事务,OPEN和CLOSE事务都是独立的,并且仅当THAT方法抛出错误时,该方法中的每个SQL操作才会回滚。如果调用它的方法抛出错误,他将不会回滚同步方法所做的任何事情。

因此,我尝试使用此示例:

@Service
public class MyService {
    @Autowired
    private MyRepository myRepository;

    public void methodNotSyncronized(String arg1, String arg2){
        logger.debug("init method no syncronied");
        MyObjct myObj = myRepository.findOne(1);

        methodSyncronized(arg2);

        myRepository.save(myObj);   //If I got some error here everything that methodSyncronized did should remaining
        logger.debug("finish method no syncronied");
    }

    @Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRES_NEW)
    private synchronized  String methodSyncronized(String arg){
        logger.debug("init method  syncronied");
        //Here I will insert or delete something    
    }

}
Run Code Online (Sandbox Code Playgroud)

但是,当我调试此代码时,我得到了:

o.h.e.t.internal.TransactionImpl         : begin
                       myService         : init method no syncronied
                       myService         : init …
Run Code Online (Sandbox Code Playgroud)

java spring-data-jpa spring-boot

2
推荐指数
1
解决办法
334
查看次数

为什么发生RuntimeException时事务不回滚?

当内部方法分别是事务性的时,我想在服务层测试非事务性方法。我想知道如果某个方法抛出异常会发生什么。它是否正确回滚?我也尝试过rollbackFor,但没有帮助。有什么建议么?

  • 春季 v-2.5.1

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create-drop

server.error.include-message=always
server.error.include-binding-errors=always
Run Code Online (Sandbox Code Playgroud)

控制器

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create-drop

server.error.include-message=always
server.error.include-binding-errors=always
Run Code Online (Sandbox Code Playgroud)

服务

@PostMapping("/test")
    public void test(){
        service.test();
    }
Run Code Online (Sandbox Code Playgroud)

java spring spring-data-jpa spring-boot

2
推荐指数
1
解决办法
3961
查看次数

@PreAuthorize在一种方法上被调用,但在另一种方法上没有被调用

在我的spring配置文件中 <global-method-security pre-post-annotations="enabled"/>

在我的春季@Controller中,我有一个@RequestMapping,其上具有一个@PreAuthorize,如下所示:

@PreAuthorize("true == false")
@RequestMapping(value="/image", method=RequestMethod.GET )
@ResponseBody
public ResponseEntity<byte[]> getImage(
        @RequestParam(value="imageSet", required=false) Long imageSetKey
        , @RequestParam(required=false, defaultValue="70") Integer size
        , @RequestParam(required=false) Unit unit
        , @RequestHeader( value="if-none-match", required=false ) String etag 
)
{
    // use the latest and greatest for the unit if they specify the unit, otherwise use the imageSetKey they pass in.
    if ( unit != null )
    {
        return getUnitImage( unit, size, etag );
    }
    // more code to do other stuff
}
Run Code Online (Sandbox Code Playgroud)

现在,此@PreAuthorize已评估并且可以正常工作。如果将PreAuthorize放在getUnitImage方法上,则不会对其进行评估,并且我可以很好地进入该方法。这是不评估@PreAuthorize的方法。 …

java spring spring-security

1
推荐指数
1
解决办法
1695
查看次数