2011-09-20 30 views
5

Tôi có một thực thể UserProfile mà tôi cần lưu. sau khi lưu thực thể trong cơ sở dữ liệu, tôi nhận được ngoại lệ sau:Bắt Giao dịch không bắt đầu thành công ngoại lệ khi sử dụng Spring Hibernate

Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started 

Ngoài ra khi tôi thấy bảng thực thể được duy trì thay vì thực hiện khôi phục!

@Transactional(isolation=Isolation.REPEATABLE_READ) 
public class HibernateUserProfileDAO implements UserProfileDAO { 
    private org.hibernate.SessionFactory sessionFactory; 
    public UserProfile getUserProfile(int userId) { 
     org.hibernate.classic.Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     UserProfile userProfile = new UserProfile(); 
     userProfile.setUserName("sury1"); 
     session.save(userProfile); 
     session.getTransaction().commit(); 
     session.close(); 
     return userProfile; 
    } 
} 

Tôi đang sử dụng quản lý giao dịch Hibernate

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

và cấu hình Hibernate của tôi là:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="com.springheatmvn.domain"/> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.connection.pool_size">10</prop> 
      <prop key="hibernate.connection.show_sql">true</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
     </props> 
    </property>  
</bean> 

Can ai pl. cho tôi biết những gì đang xảy ra ở đây?

+0

'@ user354161' xem câu trả lời đã chỉnh sửa và nhận xét từ 'Bozho' – Bitmap

Trả lời

10

Tôi nghĩ bạn đã là nạn nhân của quản lý giao dịch kép. Nếu bạn đang sử dụng Spring Transaction ManagementHibernate Transaction Management cùng nhau trong cùng một dự án, bạn có nhiều khả năng gặp sự cố này.

Mã của bạn sau đó nên thể là:

Lựa chọn 1.quản lý giao dịch Hibernate

public class HibernateUserProfileDAO implements UserProfileDAO { 
    private org.hibernate.SessionFactory sessionFactory; 
    public UserProfile getUserProfile(int userId) { 
     org.hibernate.classic.Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     UserProfile userProfile = new UserProfile(); 
     userProfile.setUserName("sury1"); 
     session.save(userProfile); 
     session.getTransaction().commit(); 
     session.close(); 
     return userProfile; 
    } 
} 

hoặc Lựa chọn 2.Quản lý giao dịch Xuân

@Transactional 
public class HibernateUserProfileDAO implements UserProfileDAO { 
    private org.hibernate.SessionFactory sessionFactory; 
    public UserProfile getUserProfile(int userId) { 
     org.hibernate.classic.Session session = sessionFactory.getCurrentSession(); 
     UserProfile userProfile = new UserProfile(); 
     userProfile.setUserName("sury1"); 
     session.save(userProfile); 
     session.close(); 
     return userProfile; 
    } 
} 
+1

yup, hoặc sử dụng @Transactional hoặc .beginTransactional(). Hơn nữa - các giao dịch mùa xuân không hỗ trợ thay đổi mức cô lập – Bozho

+0

Các giao dịch mùa xuân hỗ trợ các thay đổi mức cô lập, nhưng chỉ trong 'DataSourceTransactionManager'. Ngoài ra còn có một lớp trình bao bọc, 'org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter' mà bạn có thể sử dụng trong trình quản lý tx Hibernate. Nhưng, nếu bạn cần các mức cách ly khác nhau ở những nơi khác nhau, bạn phải quản lý các trình quản lý TX Hibernate riêng biệt, có thể được thực hiện với AOP nhưng không phải với chú thích '@ Transactional'. – AngerClown

+0

Cảm ơn các bạn !, nó đã làm việc cho tôi ngay bây giờ !!! Tôi đã sử dụng tùy chọn 2 được đề xuất bởi @Bitmap. Chỉ cần một chỉnh sửa trong tùy chọn 2, chúng tôi cũng cần xóa lệnh session.close() như sessionFactory.getCurrentSession() tự động đóng phiên. Khi tôi có câu lệnh session.close() tôi nhận được org.hibernate.SessionException: Phiên được đóng lại! ngoại lệ. Cảm ơn bạn một lần nữa! – tintin

0

Cũng có thể là xung đột giữa org.springframework.orm.hibernate3.HibernateTemplateorg.hibernate.Session.

Nếu bạn đang làm việc trên một dự án cũ, bạn có thể tìm thấy HibernateTemplate đang được sử dụng.

Nếu bạn tạo DAO mới với cách làm mới hơn, chẳng hạn như hibernate's Session và sử dụng cả DAO trên cùng một chức năng, bạn sẽ tìm thấy lỗi này.

Giải pháp: Sử dụng một thẻ còn lại. Trong trường hợp của tôi, tôi đã phải sử dụng HibernateTemplate để không thay đổi tất cả phần còn lại của chương trình cũ.

Các vấn đề liên quan