0

Not Rolling Back Spring Transaction From ZK

asked 2015-05-27 16:34:58 +0800

VikasSrivastava gravatar image VikasSrivastava
1

Dear All , I am facing a problem while calling the service method written in Spring Sevice that is injected in MVVM in that method I am saving one object and after that doing some other value fatching method call that throw exception and I want that while throwing that exception my Transaction rollback my service method part are as under CeilingServiceIMPL

            CeilingSheet ceilingSheet = new CeilingSheet();
    ceilingSheet.setFiscalYear(fiscalYear);
    ceilingSheet.setStatus(ECeilingSheetStatus.NEW);
    ceilingSheet = saveCeilingSheet(ceilingSheet).getResult();

    CeilingCategory ceilingCategory1 = null;
    try {
        ceilingCategory1 = categoryQueryService.findCeilingCategoryByCode(
                "01").getResult();
    } catch (ObjectNotFoundException ex) {
        throw new RequestException(1234, ceilingCategory1);
    }

if RequestException throws , I am expecting that my record that I saved before also rollback. I have making the service Transaction by annotating as
@Transactional(rollbackFor = RequestException.class)

When I tried this method to call from junit test case it rolling back fine , but when I integrate it with ZK zul page and View Model by autowired services it stop rolling back and even if my code throws RequestException my CeilingSheet are persisting. My ZK web.xml is

<!-- Spring can be easily integrated into any Java-based web framework. 
    All you need to do is to declare the ContextLoaderListener in your web.xml 
    and use a contextConfigLocation <context-param> to set which context files 
    to load. If you don't specify the contextConfigLocation context parameter, 
    the ContextLoaderListener will look for a /WEB-INF/applicationContext.xml 
    file to load. Once the context files are loaded, Spring creates a WebApplicationContext 
    object based on the bean definitions and puts it into the ServletContext. -->

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

<!-- LOGGER -->
<context-param>
    <param-name>log4j-config-location</param-name>
    <param-value>WEB-INF/log4j.properties</param-value>
</context-param>

<!-- Loads the Spring web application context -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- end of spring configuration -->

<description><![CDATA[My ZK Application]]></description>
<display-name>IFMIS User Interface</display-name>

<!-- //// -->
<!-- ZK -->
<listener>
    <description>ZK listener for session cleanup</description>
    <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<servlet>
    <description>ZK loader for ZUML pages</description>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>

    <!-- Must. Specifies URI of the update engine (DHtmlUpdateServlet). It 
        must be the same as <url-pattern> for the update engine. -->
    <init-param>
        <param-name>update-uri</param-name>
        <param-value>/zkau</param-value>
    </init-param>
    <!-- Optional. Specifies whether to compress the output of the ZK loader. 
        It speeds up the transmission over slow Internet. However, if you configure 
        a filter to post-processing the output, you might have to disable it. Default: 
        true <init-param> <param-name>compress</param-name> <param-value>true</param-value> 
        </init-param> -->
    <!-- [Optional] Specifies the default log level: OFF, ERROR, WARNING, INFO, 
        DEBUG and FINER. If not specified, the system default is used. <init-param> 
        <param-name>log-level</param-name> <param-value>OFF</param-value> </init-param> -->
    <load-on-startup>1</load-on-startup><!-- Must -->
</servlet>
<servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<!-- [Optional] Uncomment it if you want to use richlets. <servlet-mapping> 
    <servlet-name>zkLoader</servlet-name> <url-pattern>/zk/*</url-pattern> </servlet-mapping> -->
<servlet>
    <description>The asynchronous update engine for ZK</description>
    <servlet-name>auEngine</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>

    <!-- [Optional] Specifies whether to compress the output of the ZK loader. 
        It speeds up the transmission over slow Internet. However, if your server 
        will do the compression, you might have to disable it. Default: true <init-param> 
        <param-name>compress</param-name> <param-value>true</param-value> </init-param> -->
    <!-- [Optional] Specifies the AU extension for particular prefix. <init-param> 
        <param-name>extension0</param-name> <param-value>/upload=com.my.MyUploader</param-value> 
        </init-param> -->
</servlet>
<servlet-mapping>
    <servlet-name>auEngine</servlet-name>
    <url-pattern>/zkau/*</url-pattern>
</servlet-mapping>

My application-context.xml

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/database.properties</value>
            <value>/WEB-INF/msg-database.properties</value>
        </list>
    </property>
</bean>

<tx:annotation-driven />

<context:annotation-config />


<context:component-scan base-package="my.service" />

<import resource="hibernate-context.xml" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

My hibernat-context.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>${jdbc.driverClassName}</value>
    </property>
    <property name="jdbcUrl">
        <value>${jdbc.url}</value>
    </property>
    <property name="user">
        <value>${jdbc.username}</value>
    </property>
    <property name="password">
        <value>${jdbc.password}</value>
    </property>
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
    <property name="minPoolSize" value="${jdbc.minPoolSize}" />
    <property name="maxStatements" value="${jdbc.maxStatements}" />
    <property name="testConnectionOnCheckout" value="${jdbc.testConnection}" />
</bean>

<!-- bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName"> <value>${driverClassName}</value> </property> 
    <property name="url"> <value>${url}</value> </property> <property name="username"> 
    <value>${username}</value> </property> <property name="password"> <value>${password}</value> 
    </property> </bean -->

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>my.domain.class</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="jadira.usertype.autoRegisterUserTypes">${jadira.usertype}</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

Please advice ...

delete flag offensive retag edit

4 Answers

Sort by ยป oldest newest most voted
0

answered 2015-05-29 08:57:47 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2015-06-22 10:49:00 +0800

Hmmm, hard to understand what you mean.

Beside that, i'm wondering how you will get the 'RequestException' in your backend.

If you will more than ONE db action under the umbrella from transaction management/rollback than you should ADD a new method for this in your backend which handle the several i.e. save calls.

startTransaction
-- save 1
-- save 2
-- save 3
rollback
endTransaction


@transactional
public void saveMore(Customer customer, Order order){
   myCustomerDAO.save(customer);
   myOrderDAO.save(order);
}

best Stephan

link publish delete flag offensive edit
0

answered 2015-05-29 10:00:46 +0800

anutxxas gravatar image anutxxas
1
http://www.kredyty-gotowk...

Ill try it at home

link publish delete flag offensive edit
0

answered 2015-06-15 16:48:14 +0800

VikasSrivastava gravatar image VikasSrivastava
1

Thanks all

I found the solution : Reason : In My application we have the requirement to show the User messages based on the number thrown by the exception , for this we have a separate message-hibernate-context.xml file with seperate

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="messageSessionFactory" />
</bean>

and one is already with hibernate-context.xml

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

and the above both xml file called from my application-context.xml as

<import resource="hibernate-context.xml" />
<import resource="message-hibernate-context.xml" />

So after code thrown exception based on key thrown , my user-message bean trying to load separate Transaction Manager and old transaction committed and new Hibernate session starts that was going to rollback and since this is new Transaction so It does not found anything to rollback.

Once I removed other Transaction Manager it start working . Thanks All for support...

link publish delete flag offensive edit
0

answered 2015-06-16 08:19:16 +0800

lewerrkinna gravatar image lewerrkinna
1

nice, i hope that will work to me

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-05-27 16:34:58 +0800

Seen: 33 times

Last updated: Jun 22 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More