spring - Transactional annotation does not save on exit -


i have configured spring jpa annotation driven. expect following code persist changes database upon method exist.

@transactional public foo changevalue(int id){     final foo foo = foorepository.findone(id);     if(foo != null){        foo.setvalue("new value");        //foorepository.save(foo);     } } 

foorepository jparepository , foo object getting fetched managed. based on read @transactional i expect without foorepository.save(foo) call changes in foo's value column in database persisted upon method exist. however, happens if uncomment call foorepository.save(foo)

no exceptions thrown , configuration jpa datasource below.

  <bean id="entitymanagerfactory"  class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     ...     <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"     p:entitymanagerfactory-ref="entitymanagerfactory" />  <tx:annotation-driven transaction-manager="transactionmanager" />  <context:component-scan     base-package="com.example.package.data" />  <jpa:repositories     base-package="com.example.package.data.repository"     entity-manager-factory-ref="entitymanagerfactory"     transaction-manager-ref="transactionmanager" /> 

any ideas?

update

i have contextloaderlistener , dispatcherservlet not have component scan both.

    <servlet>     <servlet-name>appservlet</servlet-name>     <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>     <init-param>         <param-name>contextconfiglocation</param-name>         <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value>     </init-param>     </servlet>     ....     <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener> 

update

    <context-param>     <param-name>contextconfiglocation</param-name>     <param-value>/web-inf/spring/root-context.xml</param-value>      </context-param> 

and there

    <context:component-scan base-package="com.example.package">  <context:exclude-filter type="regex"          expression="com.example.package.data.*" />  <context:exclude-filter type="regex"          expression="com.example.package.web.controller.*" />      <context:exclude-filter type="regex"          expression="com.example.package.web.service.*" />        </context:component-scan> 

and in servlet-context

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

if have both contextloaderlistener , dispatcherservlet both load configuration file.

judging configuration services loaded dispatcherservlet , <tx:annotation-driven /> loaded contextloaderlistener. both create applicationcontext , aop applied beans in same context services not transactional (transaction management done using aop).

as rule of thumb contextloaderlistener should contain shared or global resources (like services, daos, datasources etc.) whereas dispatcherservlet should contain web related beans controllers, view resolvers etc.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -