jdbc连接 - 套接字连接

时间:2011-04-29 08:07:03

标签: java mysql hibernate jdbc

几天前,我的网站崩溃并向我显示了这个错误:

  

java.lang.NoClassDefFoundError:可以   没有初始化类   com.omicc.hibernate.util.HibernateUtil

所以我向托管公司询问他们可能做出的任何改变。他们修复了问题并告诉我使用JDBC连接而不是套接字连接。 我正在使用hibernate和c3p0与MySQL,据我所知他们使用JDBC连接。

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

你们这些人都知道他在说什么吗? :D(是的,他现在没有回答!)

EDITED&GT;&GT;&GT;&GT;

解决了!所以这就是我所做的 我将休眠从hibernate从3.5.0升级到3.6.1 并且新的hibernate需要hibernate-jpa-2.0-api-1.0.0.Final.jar和slf4j-simple1.6.1。 问题解决了。 我认为托管公司更新了他们的hibernate.jar,它引起了一些参考问题。

1 个答案:

答案 0 :(得分:1)

重写您的HibernateUtil,使其不在静态块中实例化。而是使其成为具有同步getInstance的单例。然后

private static SessionFactory cache;

synchronized SessionFactory getInstance() throws SQLException {

if (cache != null) return cache;

// try/catch and rethrow SQLException
try {
Class.forName("com.mysql.jdbc");
} Exception (e) {
throw new SQLException(e);
}
// Test connection with JDBC
// Create a non connection pooled raw connection, try/finally close it
// throw SQL Exception if it fails
testMe()

// finally create the sessionFactory
.... build your Configuration object
.... then 
try {
SessionFactory me = ....buildSessionFactory
} catch (RuntimeException e) {
throw new SQLException(e);
}
cache = me;
return cache;

}

一些评论:有些人会更喜欢未经检查的例外,这很好。 我喜欢做一次原始连接的原因是,在启动时,如果SQL Server碰巧完成,它往往会减少连接池/ hibernate。一旦他们成功初始化我就没有恢复问题。但这是个人品味的事情,你也可以跳过testMe()。

点是这样你会看到发生异常,我预测你会明确暗示与托管公司的联系:)