2012-05-31 4 views
0

최대 절전 모드 및 트랜잭션에 문제가 있습니다.최대 절전 모드 트랜잭션

개체 양식 DB를 삭제하고 싶습니다.

@Transactional 
public boolean addProduct(Cart cart) { 
    try { 
     sessionFactory.getCurrentSession().save(cart); 
     return true; 
    } catch (HibernateException e) { 
     LOG.debug("ERROR adding product into cart" + e); 
     return false; 
    } 

} 

@Transactional 
public boolean deleteProductByProdIdAndAmount(Cart cart) {  
    sessionFactory.getCurrentSession().delete(cart); 
    return true; 
} 

완벽하게 개체를 추가하지만 삭제하지 않으며 예외를받지 못합니다. 내가 쓸 경우

sessionFactory.getCurrentSession().getTransaction().commit(); 

이 표시됩니다. 트랜잭션이 성공적으로 시작되지 않았지만 객체가 삭제된다는 예외가 표시됩니다.

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

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


<bean id="configProperties" class="com.dataart.masternoy.utils.PropertiesUtil"> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:/config.properties</value> 
      <value>classpath:/jdbc.properties</value> 
     </list> 
    </property> 
</bean> 

<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/shop" /> 
    <property name="username" value="root" /> 
    <property name="password" value="root" /> 
</bean> 

<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> 
    <constructor-arg ref="dataSource" /> 
</bean> 

<bean id="txManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

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


<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
    lazy-init="true"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernateConfig.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> 
      <prop key="hibernate.connection.charSet">UTF-8</prop> 


     </props> 
    </property> 
</bean> 

내가 그것을 DB에서 개체를 삭제합니다

sessionFactory.getCurrentSession().createQuery("delete from Cart where order_id = 77 and product_id = 9").executeUpdate(); 

을 작성하는 경우 : 여기 내 설정 파일입니다.

+1

어디 addProduct()와 deleteProductByProdIdAndAmount()를 호출하는 코드? 그것은 @Transactional ...으로 표시되어야합니다. 그리고 Spring을 사용하고 있으므로 commit() 또는 rollback()을 호출 할 필요가 없습니다. –

+0

예 ** 인터페이스 **에서 트랜잭션 주석이 있습니다. 그리고 나는 봄이 나를 위해 많은 일을해야한다는 것을 압니다. –

답변

0

삭제하기 전에 영구 층에서 cart을 얻고 있습니까? 트랜잭션 경계 설정을 서비스 계층 또는 클라이언트 코드로 옮길 수도 있습니다. 예 :

@Transactional 
public void deleteCart(long id) { 
    Cart cart = this.cartDao.getCart(id); 
    this.cartDao.deleteProductByProdIdAndAmount(cart); 
} 
관련 문제