2009-10-28 147 views
0

這是我在總結實施春季交易在不同的DAO不起作用嗎?

1)所有的DAO實現並採用的HibernateDAO支持/ @Transational註釋僅在服務層

2)使用主要使用MySQL/HibernateTransactionManager的

3)測試(使用String args [])方法(使用這種方法做事務工作嗎?)

事務沒有得到回滾,並且在數據庫中可以看到無效的entried。 我在這裏做錯了什麼?

詳細信息如下。

1)我已經配置使用@Transactional註釋在服務層事務處理:使用主

<bean id="dataSource" 
     class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" 
      value="jdbc:mysql://localhost:3306/ibmdusermgmt" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
     <property name="defaultAutoCommit" value="false"/>  
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" />  
     <property name="mappingLocations" 
      value="classpath*:hibernate/*.hbm.xml" /> 
     <property name="hibernateProperties"> 
       <prop key="hibernate.dialect"> 
       org.hibernate.dialect.MySQLDialect 
       </prop>    
       <prop key="hibernate.query.substitutions"> 
        true=1 false=0 
       </prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.use_outer_join">false</prop> 
      </props> 
     </property> 
    </bean> 

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

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

3)試驗:

@Transactional(readOnly=false, rollbackFor={DuplicateEmailException.class,DuplicateLoginIdException.class,IdentityException.class},propagation=Propagation.REQUIRES_NEW) 
    public void createUserProfile(UserProfile profile) 
      throws DuplicateEmailException, DuplicateLoginIdException, 
      IdentityException { 

     //Accessing DAO (implemented using HibernateDAOSupport) 
       identityDAO.createPrincipal(profile.getSecurityPrincipal()); 
     try { 
       //Accessing DAO 
      userProfileDAO.createUserProfile(profile); 
     } catch (RuntimeException e) { 
      throw new IdentityException("UseProfile create Error", e); 
     } 

    } 

2)我TRANSATION管理器配置/數據源被如下()方法如下:

public static void main(String[] args) { 

     ApplicationContext cnt=new ClassPathXmlApplicationContext("testAppContext.xml"); 
     IUserProfileService upServ=(IUserProfileService)cnt.getBean("ibmdUserProfileService"); 

     UserProfile up=UserManagementFactory.createProfile("testlogin");//  
     up.setAddress("address"); 
     up.setCompany("company"); 
     up.setTelephone("94963842"); 
     up.getSecurityPrincipal().setEmail("[email protected]"); 
     up.getSecurityPrincipal().setName("Full Name"); 
     up.getSecurityPrincipal().setPassword("password"); 
     up.getSecurityPrincipal().setSecretQuestion(new SecretQuestion("Question", "Answer")); 

     try { 
      upServ.createUserProfile(up); 
     } catch(Exception e){ 
      e.printStackTrace(); 
     } 

回答

1

據我所知,MySQL的defa超級存儲引擎MyISAM does not support transactions

MySQL服務器(版本3.23-max和所有版本4.0及以上版本)支持與InnoDB和BDB事務存儲引擎的交易。 InnoDB提供完整的ACID合規性。 ...在MySQL服務器中的其他非事務性存儲引擎(如MyISAM)遵從數據完整性不同的範例,稱之爲「原子操作」。按照事務術語,MyISAM表總能高效地在自動提交= 1種模式

操作。在爲了使事務正常工作,您必須將這些表的存儲引擎切換到InnoDB。您可能還希望,如果你從你的映射生成你的表(hdm2ddl)使用Hibernate的MySQLInnodDBDialect

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> 

正如@gid提到的,它不是交易的要求,但。

1

是的。您可以從main()調用交易對象。

@sfussenegger是正確的,MyISAM不支持事務,使用MySQLDialect方言不排除使用事務,它只意味着hdm2ddl表生成將創建InnoDB類型的表。 (這當然可能是你的問題)。

你能確認你在MySQL中使用的是事務類型表嗎?

假設這是真的,接下來要做的是從org.springframework.transaction.support.AbstractPlatformTransactionManager得到一些調試輸出。如何做到這一點取決於你正在使用的日誌框架,但它應該是直截了當的。

+0

由於GID, 我配置成使用 <類別名稱= 「org.springframework.transaction.support.AbstractPlatformTransactionManager」> <優先級值= 「INFO」/> 我不能看到像任何記錄的「創建的log4j新名稱的交易「。 ? – 2009-10-28 08:07:43

+0

下一步是檢查您從上下文獲得的IUserProfileService實例是否是代理,並且該代理具有對TransactionInterceptor的引用,最好在調試器中執行此操作 – 2009-10-28 08:53:16