JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
話題被移動
該話題已被移動 - koji , 2008-11-20 12:22
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 请教Hibernate 及 Spring 事务配置问题
leon_fan





發文: 11
積分: 0
於 2008-11-20 11:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最近在配置HIBERNATE+SPRING事务的时候总是出现问题,想向大家请教一下
具体表现为两个REQUIRED函数连续调用,第一个函数调用成功,第二个函数调用抛出异常,但是第一个函数没有ROLLBACK而是COMMIT了,不符合事物传播的规则
查看日志也是这样,第一个函数在调用完成后就COMMIT了,而没有保持和第二个函数在同一个transaction中
具体配置如下:
请大家无论如何都要帮我下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
        <property name="username" value="sys"/>
        <property name="password" value=""/>
    </bean>
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
          <list>
            <value>entity/Person.hbm.xml</value>
          </list>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" />
            <tx:method name="delete" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:advisor pointcut="execution(* business.*.*(..))" advice-ref="txAdvice"/>
    </aop:config>
    
    
    
    <bean id="person" class="business.PersonImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
</beans>
 


1
2
3
4
5
6
7
8
9
10
11
package business;
 
import org.hibernate.SessionFactory;
 
public interface Person {
    
    public void save(entity.Person person);
    public void delete(entity.Person person);
 
}
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package business;
 
import org.hibernate.SessionFactory;
 
public class PersonImpl implements Person {
    
    private SessionFactory sf;
    
    public void setSessionFactory(SessionFactory sf) {
        this.sf = sf;
    }
    
    public void save(entity.Person person) {
        sf.getCurrentSession().save(person);
    }
    
    
    public void delete(entity.Person person) {
        throw new UnsupportedOperationException();
    }
 
}
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.orm.hibernate3.HibernateTemplate;
 
import entity.Person;
 
public class PersonBusiness {
    
    public static void main(String[] args) throws Exception {
 
        
        ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
        business.Person p = (business.Person) ctx.getBean("person");
        
        Person person = new Person();
        p.save(person);
        p.delete(person);
        
    }
 
}
 


reply to postreply to post
作者 Re:请教Hibernate 及 Spring 事务配置问题 [Re:leon_fan]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-11-20 12:21 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你應該是搞錯了..

你雖然有對business做transaction套用
但是你設save, delete,是想設在PersonImpl上?
你應該要設在PersonBusines的某個method上
transaction設定應該是要對應到service method,而非dao method.

呼叫也應該透過PersonBusines main呼叫被套用的method,然後那個method內有save, delete動作

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
» JWorld@TW »  Java Application Framework » Spring

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8