我是 Hibernate 的新手,在使用Hibernate时,我遇到了IllegaStateException几次,异常不一致,因为我不知道它发生在什么特定条件下,有时它永远不会发生,但有时它会发生,我无法弄清楚如何解决它,请对此有所了解。
May 14, 2013 5:32:40 PM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already. Could not load com.mysql.jdbc.ProfilerEventHandlerFactory. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4412)
at com.mysql.jdbc.ConnectionImpl.close(ConnectionImpl.java:1564)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.stop(DriverManagerConnectionProviderImpl.java:160)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.finalize(DriverManagerConnectionProviderImpl.java:229)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:101)
at java.lang.ref.Finalizer.access$100(Finalizer.java:32)
at java.lang.ref.Finalizer$FinalizerThread.run
(Finalizer.java:178)
这是非常令人不安的。此外,我正在使用我创建的包装器之一来处理 Sessions 和 SessionFactory。我还检查了我的代码,每次我在内部使用Hibernate api的 dao中使用时都使用了closeSession() 。
这是我的ManagerSessionFactory(包装器)
public class ManagerSessionFactory {
private static SessionFactory sessionFactory;
private static Configuration configuration;
protected static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static void initializeSessionFactory(String configFilePath) {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
configuration = new Configuration();
sessionFactory = configuration.configure(configFilePath).buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
private static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session createSession() {
return createSession(null);
}
public static Session createSession(String configFilePath) {
if (configFilePath == null || configFilePath.trim().equals("")) {
configFilePath = "/hibernate.cfg.xml";
}
SessionFactory localSessionFactory = ManagerSessionFactory.getSessionFactory();
Session session = ManagerSessionFactory.threadLocal.get();
if (session == null || !session.isOpen()) {
if (localSessionFactory == null) {
try {
ManagerSessionFactory.initializeSessionFactory(configFilePath);
localSessionFactory = ManagerSessionFactory.getSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%% " + e.getMessage());
}
}
session = localSessionFactory.getCurrentSession();
System.out.println("Session Opened......");
ManagerSessionFactory.threadLocal.set(session);
}
return session;
}
public static void closeSession(){
closeSession((Session)ManagerSessionFactory.threadLocal.get());
return;
}
public static void closeSession(Session session){
if (session != null && session.isOpen()) {
session.close();
//ManagerSessionFactory.threadLocal.
}
session = null;
return;
}
}
这是ManageTransaction,它扩展了前一个类的功能:
public class ManageTransaction extends ManagerSessionFactory {
private Session session;
private Transaction transaction;
private String configFilePath;
private boolean toStartTransaction;
public ManageTransaction() {
this.session = ManagerSessionFactory.createSession();
initManagerTransaction("", false, session);
}
public ManageTransaction(boolean toStartTransaction) {
this.session = ManagerSessionFactory.createSession();
if (toStartTransaction) {
this.transaction = this.session.getTransaction();
this.transaction = this.transaction.isActive() ? this.transaction : this.session.beginTransaction();
}
initManagerTransaction("", toStartTransaction, session);
}
public ManageTransaction(boolean toStartTransaction, String configFilePath) {
if (toStartTransaction) {
this.session = ManagerSessionFactory.createSession();
this.transaction = this.transaction.isActive() ? this.transaction : this.session.beginTransaction();
}
initManagerTransaction(configFilePath, toStartTransaction, this.session);
}
public ManageTransaction(String configFilePath, boolean toStartTransaction) {
ManagerSessionFactory.initializeSessionFactory(configFilePath);
if (toStartTransaction) {
this.session = ManagerSessionFactory.createSession();
this.transaction = this.transaction.isActive() ? this.transaction : this.session.beginTransaction();
}
initManagerTransaction(configFilePath, toStartTransaction, this.session);
}
public ThreadLocal<Session> getThreadLocal() {
return threadLocal;
}
private void initManagerTransaction(String configFilePath, boolean toStartTransaction, Session session) {
this.configFilePath = configFilePath == null ? "" : configFilePath;
this.toStartTransaction = toStartTransaction;
ManageTransaction.threadLocal.set(session);
}
public void closeManageTransaction() {
afterEveryOperation(true);
ManagerSessionFactory.closeSession(this.getSession());
}
}
这是我的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/befundo?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--mapping classes -->
</session-factory>
</hibernate-configuration>
提前致谢。