使用Spring Hibernate获取事务未成功启动异常
我有一个需要保存的UserProfile实体。将实体保存到数据库中后,出现以下异常:使用Spring Hibernate获取事务未成功启动异常,hibernate,spring,spring-mvc,Hibernate,Spring,Spring Mvc,我有一个需要保存的UserProfile实体。将实体保存到数据库中后,出现以下异常: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started 此外,当我看到表时,实体被持久化,而不是执行回滚 @Transactional(isolation=Isolation.REPEATABLE_RE
Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
此外,当我看到表时,实体被持久化,而不是执行回滚
@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;
}
}
我正在使用hibernate事务管理器
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
我的hibernate配置是:
<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>
10
真的
org.hibernate.dialogue.mysqldialogue
更新
有人能告诉我这里发生了什么吗?我想你是双重交易管理的受害者。如果在同一项目中同时使用
Spring事务管理
和Hibernate事务管理
,则更可能出现此问题
然后,您的代码应该是:
选项1。休眠事务管理
或选项2。Spring事务管理
也可能是
org.springframework.orm.hibernate3.HibernateTemplate
和org.hibernate.Session
之间的冲突
如果您正在处理一个旧项目,您可能会发现正在使用HibernateTemplate
如果您使用更新的方式创建一个新的DAO,如hibernate的会话
,并在同一个函数上使用两个DAO,您将发现此错误
解决方案:使用其中一种。在我的例子中,我必须使用
HibernateTemplate
,以避免更改旧程序的所有其他部分。是的,可以使用@Transactional或.beginTransactional()。此外,spring事务不支持更改隔离级别spring事务支持隔离级别更改,但仅在DataSourceTransactionManager中支持。还有一个包装类,org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter
,您可以在Hibernate tx管理器中使用。但是,如果您在不同的位置需要不同的隔离级别,您必须管理单独的Hibernate TX管理器,这可以通过AOP完成,但不能通过@Transactional
注释完成。谢谢大家!,它现在对我起作用了!!!我使用了@Bitmap建议的选项2。选项2中的一个更正是,我们还需要删除session.close()语句,因为sessionFactory.getCurrentSession()会自动关闭会话。当我有session.close()语句时,我得到了org.hibernate.SessionException:会话已关闭!例外。再次感谢@user354161
参见编辑的答案和评论,来自Bozho
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;
}
}
@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;
}
}