我有一个独立的线程应用程序。哪个是等待消息的监听器,当它到达时做某些事情,我必须在DB中保存消息。 但我有问题,因为如果我运行应用程序并“手动发送消息”,每个事情都可以正常工作,但如果我运行应用程序并等待系统的消息(例如,它到达后一小时)APP的去保存到数据库,它告诉:
java.sql.SQLException:Io异常:连接重置 要么 java.sql.BatchUpdateException:Io Exception:Connection reset
我正在使用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>
我做错了什么? 提前致谢
答案 0 :(得分:1)
为什么在尝试保存数据之前不创建新会话?根据Hibernate的文档,这是原子操作的正确方法。正如我可以在代码中看到您的注释,您认为启动事务意味着启动新会话或打开新连接whitch是不正确的。每个会话可以有多个(但不总是嵌套的)事务。 我总是使用以下模板进行原子操作 - 它永远不会让我失望。我甚至在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
对象。创建它很昂贵。