HibernateException: Couldn't obtain transaction-synchronized Session for current thread

后端 未结 4 1211
无人共我
无人共我 2020-12-01 16:40

I\'m getting the following exception when trying to use my @Service annotated classes:

org.hibernate.HibernateException: Could not obtain transa         


        
4条回答
  •  广开言路
    2020-12-01 17:23

    One

    You must use @Transactional for @Service and @Repository. It lets Spring apply and create proxies with Transaction support.

    In your code your @Service class has no the @Transacional either in class level or method level

    Second

    Where is the class that implements WebApplicationInitializer? I see you are extending a class.. Anyway My Point is, where is something like the following:

    @Override
    public void onStartup(ServletContext container) {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(CentralServerConfigurationEntryPoint.class);
    
        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));
    
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(CentralWebConfigurationEntryPoint.class);
    
        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    
    }
    

    Where CentralServerConfigurationEntryPoint.class must only scan components that must work in the server side (@Service, @Repository, @Configuration for Transaction, Hibernate, DataSource etc)

    Where CentralWebConfigurationEntryPoint must only scan components that must work in the client/web side (@Controller, @Configuration for Formatters, Tiles, Converters etc)

    I dont understand your code about

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    
        ConfigurableEnvironment environment = rootContext.getEnvironment();
        environment.setDefaultProfiles("production");
    
        PropertyUtil propertyUtil = PropertyUtil.getInstance(environment.getActiveProfiles());
        String[] basePackages = propertyUtil.getPropertySplitTrimmed("webapp", "basePackages");
        rootContext.scan(basePackages);
    
        return rootContext;
    }
    
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        return new AnnotationConfigWebApplicationContext();
    }
    

    My point is: you must have two AnnotationConfigWebApplicationContext one for the server and web side.

提交回复
热议问题