[英]Connection reset in long session
我有一个独立的线程应用程序。 这是一个等待消息的侦听器,到达消息后便做一些事情,其中我必须将消息保存在数据库中。 但是我遇到了问题,因为如果我运行该应用程序并“手动发送一条消息”,那么一切都可以正常工作,但是如果我运行该应用程序并在系统运行时等待系统消息(例如,到达后一小时)保存到下沉的数据库,告诉:
java.sql.SQLException:Io异常:连接重置或java.sql.BatchUpdateException:Io异常:连接重置
我正在使用Hibernate 3.2.6和C3p0 0.9.2.1
会话的配置为:
public final class PersistenceUtil{
private static final SessionFactory sessionFactory;
static {
try {
String location = ServiceLocator.getInstance().getConfigurationService().getProperty("adaptor.location");
if (LOC_1.equals(location)) {
sessionFactory = new AnnotationConfiguration().configure("hibernate.1.cfg.xml").buildSessionFactory();
} else if(LOC_2.equals(location)) {
sessionFactory = new AnnotationConfiguration().configure("hibernate.2.cfg.xml").buildSessionFactory();
}else {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
sessionFactory.openSession();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private PersistenceUtil() {
}
public static void shutdown() {
try {
sessionFactory.close();
} catch (HibernateException e) {
LOG.error("PersistanceUtil.shutdown Error: " + e.getMessage());
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
当我想保存时(错误在哪里):
public T save(T entity) {
if (!getSession().getTransaction().isActive()) {
log.warn("Session not active. Starting the session");
getSession().beginTransaction();
}
getSession().save(entity);
getSession().getTransaction().commit();
return entity;
}
我的hibernate.cfg.xml是:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">URL</property>
<property name="connection.username">USER</property>
<property name="connection.password">Password</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">0</property>
<mapping class="MessageEO" />
<mapping class="CustomerEO" />
</session-factory>
</hibernate-configuration>
我究竟做错了什么? 提前致谢
为什么在尝试保存数据之前不创建新会话? 根据Hibernate的文档,这是原子操作的正确方法。 正如我在代码中看到的注释所示,您认为启动事务意味着启动新会话或打开新连接异常是不正确的。 每个会话中可以有多个(但不总是嵌套的)事务。 我始终使用以下模板进行原子操作-它从不让我失望。 我什至在Eclipse中将这段代码作为模板:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// save/ update / delete your entities here
tx.commit();
} catch (RuntimeException ex) {
if (tx != null) {
tx.rollback();
}
throw ex;
} finally {
session.close();
}
原则:
SessionFactory
对象。 创建它很昂贵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.