java - 不同 DAO 中的 Spring 事务无论如何都不起作用?

标签 java mysql spring hibernate transactions

这是我的实现摘要

1) 所有使用 HibernateDAO 支持实现的 DAO/@Transational 注解仅在 Service 层使用

2) 使用 MySQL/HibernateTransactionManager

3) 使用 main(String args[]) 方法进行测试(使用此方法交易是否有效?)

事务不会回滚,并且在数据库中可以看到无效的条目。 我在这里做错了什么?

详细信息如下。

1)我在服务层中使用@Transactional注释将事务配置为:

@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 Manager 配置/数据源如下:

<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)使用main()方法进行测试,如下:

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("security@email.com");
        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();
        }

最佳答案

据我所知,MySQL的默认存储引擎MyISAM does not support transactions

MySQL Server (version 3.23-max and all versions 4.0 and above) supports transactions with the InnoDB and BDB transactional storage engines. InnoDB provides full ACID compliance. ... The other nontransactional storage engines in MySQL Server (such as MyISAM) follow a different paradigm for data integrity called “atomic operations.” In transactional terms, MyISAM tables effectively always operate in autocommit = 1 mode

为了使事务正常工作,您必须将这些表的存储引擎切换为 InnoDB。您可能还想使用 Hibernate 的 MySQLInnodDBDialect如果您从映射生成表 (hdm2ddl):

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

正如 @gid 所提到的,这并不是事务的要求。

关于java - 不同 DAO 中的 Spring 事务无论如何都不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1635633/

相关文章:

java - Java中的抽象类和接口(interface)

c# - ASP.NET 中的 GridView 问题

java - 安全:filter-chain pattern match url have symbol '?'

java - 提交到数据库时执行一些常见的操作

java - 如何使用 Web 客户端在 Apache CXF JAX-RS 中发送带有正文的 DELETE 请求?

mysql - 排序依据,在末尾放置 0,同时保持升序搜索顺序

php - MySQL 使用具有多个 LIKE 的 WHERE 子句

java - 是否有任何开箱即用的 spring ingetration 模式用于使用动态队列中的消息并处理它们?

java - 如何优化hibernate循环中的方法调用?

java - 如何循环二维数组中的元素以在 "Ruby functional Style"中构造字符串