简体   繁体   English

无论如何,不​​同DAO中的Spring事务仍然无法工作?

[英]Spring Transactions in different DAOs does not work anyway?

Here is my implementation in summary 这是我的实现总结

1) all DAOs implemented using HibernateDAO support/ @Transational annotation is only used in Service layer 1)所有使用HibernateDAO支持/ @Transational注释实现的DAO仅在服务层中使用

2) Use MySQL / HibernateTransactionManager 2)使用MySQL / HibernateTransactionManager

3) Test using main(String args[]) method (do transactions work using this method? ) 3)使用main(String args [])方法进行测试(交易是否可以使用此方法?)

Transactions does not get rollbacked and invalid entried can be seen in the database. 事务不会回滚,并且在数据库中可以看到无效的输入。 What am I doing wrong here? 我在这里做错了什么?

Detailed information is given below. 详细信息如下。

1) I have configured transactions using @Transactional annotation in service layer as: 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) My Transation Manager configuration / datasource is as follows : 2)我的翻译管理器配置/数据源如下:

<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) Test using main() method as follows : 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();
        }

As far as I know, MySQL's default storage engine MyISAM does not support transactions . 据我所知,MySQL的默认存储引擎MyISAM不支持事务

MySQL Server (version 3.23-max and all versions 4.0 and above) supports transactions with the InnoDB and BDB transactional storage engines. MySQL服务器(版本3.23-max和所有版本4.0及更高版本)支持使用InnoDB和BDB事务存储引擎的事务。 InnoDB provides full ACID compliance. InnoDB提供完全的ACID合规性。 ... 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 ... MySQL服务器中的其他非事务性存储引擎(例如MyISAM)遵循不同的数据完整性范式,称为“原子操作”。在事务方面,MyISAM表始终始终以autocommit = 1模式运行

In order to get transactions working, you'll have to switch your storage engine for those tables to InnoDB. 为了使事务正常运行,您必须将这些表的存储引擎切换到InnoDB。 You may also want to use Hibernate's MySQLInnodDBDialect if you generate your tables ( hdm2ddl ) from your mappings: 如果从映射生成表( hdm2ddl ),则可能还需要使用Hibernate的MySQLInnodDBDialect

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

As mentioned by @gid, it's not a requirement for transactions though. 正如@gid所提到的,虽然这不是交易的要求。

Yes. 是。 you can call transactional objects from main() . 您可以从main()调用事务对象。

@sfussenegger is correct in that MyISAM doesn't support transactions, use of the MySQLDialect dialect doesn't exclude the use of transactions it only means that the hdm2ddl table generation will create InnoDB type tables. @sfussenegger是正确的,因为MyISAM不支持事务,使用MySQLDialect方言并不排除使用事务,这仅意味着hdm2ddl表生成将创建InnoDB类型表。 (which of course could be your issue). (当然这可能是您的问题)。

Can you confirm you are using a transactional type table in MySQL? 您可以确认您正在使用MySQL中的事务类型表吗?

Assuming this is true the next thing to do is get some debug output from org.springframework.transaction.support.AbstractPlatformTransactionManager . 假设这是真的,那么下一步是从org.springframework.transaction.support.AbstractPlatformTransactionManager获得一些调试输出。 How to do this depends on which logging framework you are using but it should be straight forward. 如何执行此操作取决于您使用的日志记录框架,但是应该很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM