问题描述
我有一个UserProfile实体,我需要保存。在将实体保存到数据库后,我得到以下异常:
无法提交Hibernate事务;嵌套异常是org.hibernate.TransactionException:事务没有成功启动
另外,当我看到表的实体
$ b $> $ code $ @ $ Transaction $ {
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();
返回userProfile;
我正在使用hibernate事务管理器
< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
和我的hibernate配置是:
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =com.springheatmvn.domain/>
< property name =hibernateProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>
任何人都可以。告诉我这里发生了什么事情?
我认为您已经成为双重交易管理的受害者。如果您在同一个项目中同时使用 Spring Transaction Management
和 Hibernate Transaction Management
,那么您更有可能拥有这个问题。
您的代码应该是:
选项1。 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();
返回userProfile;
}
}
或选项2。 Spring事务管理
@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();
返回userProfile;
}
}
I have a UserProfile entity which I need to save. after saving the entity in the database, I get the following exception:
Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
Also when I see the table the entity is persisted instead of doing a rollback!
@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;
}
}
I'm using hibernate transaction manager
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
and my hibernate config is:
<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 anyone pl. tell me what's going on here?
I think you've fall a victim of dual transaction managements. If you are using Spring Transaction Management
and Hibernate Transaction Management
together in the same project, you are more likely to have this issue.
Your code then should either be:
Option 1. Hibernate transaction management
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;
}
}
or Option 2. Spring transaction Management
@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;
}
}
这篇关于使用Spring Hibernate获取事务未成功启动异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!