在spring4中应用Annotation驱动的Jdbc-transaction

时间:2016-02-10 12:54:12

标签: java spring transactions

我正在尝试在我的spring mvc项目中定义事务,但遇到问题:

// xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns=
        "http://www.springframework.org/schema/beans"
    xmlns:xsi=
        "http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context
        ="http://www.springframework.org/schema/context"
    xmlns:p=
        "http://www.springframework.org/schema/p"
    xmlns:tx=
        "http://www.springframework.org/schema/tx"
    xsi:schemaLocation=
        "http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Enable Annotation based Declarative Transaction Management -->
    <tx:annotation-driven transaction-manager="transactionManager" />

<!-- Creating TransactionManager Bean, since JDBC we are creating of type DataSourceTransactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

<!-- Initialization for data source -->
    <bean id="dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">      
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="jdbc:mysql://localhost/bhaiyag_devgrocery"></property>
      <property name="username" value="newuser"></property>
      <property name="password" value="kim"></property>
   </bean>


   <bean id="CategoriesDaoImpl" class="org.kmsg.dao.daoImpl.CategoriesDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

  <bean id="CityDaoImpl" class="org.kmsg.dao.daoImpl.CityDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="CustomerDaoImpl" class="org.kmsg.dao.daoImpl.CustomerDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 

 </beans>

//这是我想要应用交易的类

public class CustomerOrderAdapter 
{
    public void displayCustomerOrder(String mobileNo) 
    {     }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = true)  
    public Map<String, Object> createCustomerOrderData(SalesCommandObject salesCommandObject,long OrderForDelete) throws Exception
    {
        Map <String, Object> data = new HashMap<String, Object>();  
    try
        {
                salesDaoImpl.deleteSalesitems(OrderNo);
                salesDaoImpl.deleteSales(OrderNo);
                             salesDaoImpl.insertSalesItems(updatedOn,OrderNo,CustmobileNo);

salesDaoImpl.insertSales(totalSale, SlotNo, OrderNo);

        }
        catch(IndexOutOfBoundsException ex)
        {
            System.out.println("No Record to Save");    
            data.put("status", "No Record to Save");
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            data.put("status", e);
        }
        data.put("status", "Purchase Successfull");
        return data;
    }

}

//帮帮我并建议,如何实现交易;

0 个答案:

没有答案