2009-10-28 84 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