【发布时间】:2015-04-14 15:42:08
【问题描述】:
首先,如果本网站有与我的问题相关的内容,我想道歉。我什么也找不到。 我正在开发一个 spring+hibernate 应用程序,该应用程序链接到用 AngularJS 编写的前端。 例如,当我从前端调用特定操作时:
@MessageMapping("/markVoicemailAsRead/{uniqueId}")
public void markRead(@DestinationVariable int uniqueId) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
voiceMailDao.setVoiceMailRead(uniqueId);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
一切都很好,它可以在我的 postgres 数据库中正常工作。我发现奇怪的是,在setVoiceMailRead(uniqueId) 中我必须打开另一个会话,而且我还需要一个事务对象。这是setVoiceMailRead(uniqueId)的实现:
public void setVoiceMailRead(int id) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
Query query = session.createQuery("update VoiceMail set read = :read"
+ " where uniqueId = :id");
try {
transaction = session.beginTransaction();
query.setParameter("read", true);
query.setParameter("id", id);
query.executeUpdate();
transaction.commit();
} catch (HibernateException e) {
if (transaction != null)
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
如果我在 markRead 操作中删除会话 ant 事务,我会在控制台“无活动事务”中收到错误,所以我就这样离开了。我认为我的hibernate.xml 文件和<property> 标签有问题:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
提前感谢您的回复!
【问题讨论】:
标签: java hibernate postgresql session transactions