2013年11月14日星期四

hibernate4 dialects and transaction issues

Recently made ​​a learning framework springMVC + hibernate4
because spring3.x recommend using hibernate after their session management
So sessionFactory configuration used

abandoned the original spring templete acquisition mode .
DAO layer directly on the desired opening session

Session session = sessionFactory.openSession ();
which is configured dialect hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

But session.persist (bo); This method can not do so in the new data , under the guidance of the problem in big trouble God grateful .

transaction issues :

I have a method in the dao layer before adding :
@ Transactional (readOnly = false)
public void executeSQL (String sql) {


and baseDao.executeSQL (sql);
throw new RecoverableDataAccessException ("INSERT") ;
executed throw an exception, but the database rollback information but did not do so , the data has been persisted. Knees and begged the god guidance problem.
------ Solution ---------------------------------------- ----
configuration HQL statement is not automatically submitted to a transaction.
------ For reference only -------------------------------------- -


persist do not use a transaction ?
------ For reference only -------------------------------------- -
problem has been solved, the problem lies in the use of the emergence and hibernate jpa dialect corresponding method when there is a difference , persist meaning persistent , corresponding to jpa, and hibernate corresponding preservation method save ();

The current problem two still not been resolved :
 <tx:annotation-driven transaction-manager="transactionManager"/> 
    <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg index="0" ref="dataSource">
</constructor-arg>
</bean>


add annotations are transaction methods , respectively dao serviece before adding a comment :
@Transactional(readOnly=false,rollbackFor=Exception.class) 


still throws an exception when there is no rollback transaction. very confused god seeking guidance
------ For reference only ------------------------------- --------
problem has been resolved ... two cups postings end, or themselves , oh

configuration file:
<tx:annotation-driven transaction-manager="transactionManager"/> 
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean> 
    
    <!-- 需要引入tx的命名空间 -->  
    <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
         <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->  
         <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
           <tx:method name="get*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="query*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="*" propagation="REQUIRED" />  
        </tx:attributes>  
    </tx:advice> 


 //Session session = sessionFactory.openSession();
        Session session = sessionFactory.getCurrentSession();


session Obtaining changed so OK you can freely control rolled back !

没有评论:

发表评论