0
votes

/WEB-INF/appconfig-data.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/javabegin" />
    <property name="username" value="root" />
    <property name="password" value="pass" />
</bean>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">



    <property name="driverClass" value="${jdbc.driverClassName}" />

    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <property name="minPoolSize" value="3" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.hihoall" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="myTransactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager" />

/WEB-INF/appconfig-root.xml

<import resource="spring-mvc-servlet.xml"/>

<import resource="appconfig-data.xml"/>

<import resource="application-security.xml"/>

<context:component-scan base-package="com.hihoall.*"/>

<context:property-placeholder location="classpath:database.properties"/>

/WEB-INF/application-security.xml

<http auto-config="true">

    <remember-me data-source-ref="dataSource" />


    <access-denied-handler error-page="/accessDenied" />

    <intercept-url pattern="/user" access="ROLE_USER" />
    <intercept-url pattern="/admin" access="ROLE_ADMIN" />

    <form-login login-page='/login' default-target-url="/user"
                authentication-failure-url="/login?error=true" username-parameter="user_login"
                password-parameter="password_login" />

    <logout logout-success-url="/login" />

</http>

<beans:bean id="jdbcGroupsImpl" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <beans:property name="enableGroups" value="true" />
    <beans:property name="enableAuthorities" value="false" />
    <beans:property name="dataSource" ref="dataSource" />
</beans:bean>

<authentication-manager>
    <authentication-provider  user-service-ref="jdbcGroupsImpl">

    </authentication-provider>
</authentication-manager>

/WEB-INF/spring-mvc-servlet.xml

<mvc:annotation-driven/>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

<mvc:resources mapping="/favicon.ico" location="/resources/images/favicon.ico" />

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <ref bean="localeChangeInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/locales/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>


<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="ru" />
</bean>

<context:component-scan base-package="com.hihoall"/>

/WEB-INF/web.xml

    <display-name>spring-mvc</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/appconfig-root.xml
    </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Everything works well, but If I change it a little bit

The stroke "/WEB-INF/spring-mvc-servlet.xml" has been added to web.xml

<display-name>spring-mvc</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/appconfig-root.xml
    </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/spring-mvc-servlet.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

and the stroke "import resource="spring-mvc-servlet.xml" has been deleted from /webapp/WEB-INF/appconfig-root.xml

    <import resource="appconfig-data.xml"/>

    <import resource="application-security.xml"/>

    <context:component-scan base-package="com.hihoall.*"/>

    <context:property-placeholder location="classpath:database.properties"/>
</beans>

I get the error

HTTP Status 500 - org.apache.jasper.JasperException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

What should I do to make the second variant working?

1

1 Answers

1
votes

You have component-scanning in mvc config file as well as data config file:

Inside /WEB-INF/spring-mvc-servlet.xml:

<context:component-scan base-package="com.hihoall"/>

Inside /WEB-INF/appconfig-root.xml

<context:component-scan base-package="com.hihoall.*"/>

Since they are scanning the same packages, then the beans are loaded 2 times here, once by mvc config & another by root config. So they are stored in Web Application Context and another in Root Application Context.

Now the transaction management beans are defined in root config file (because you are importing /WEB-INF/appconfig-data.xml in root config file).

This is causing issue with your application, the controller classes might got the beans from web application context which do not have an idea about transaction management, so when you are doing DML operations it is failing with that error.