Struts2 と Hibernate を使用してアプリケーションを作成しました。しかし今、私は問題に直面しています。プロジェクト ページを初めて閲覧すると、すべてのデータがデータベースから正しく表示されます。しかし、自分のページを閲覧し続けると、数分/時間後に、データベース テーブルのデータを自分の html ページに表示できなくなります。(私のDAOクラスはテーブルの値を与えることができず、次のようなエラーをスローします
Exception in getSubjects() :org.hibernate.exception.JDBCConnectionException: could not execute query
)または、アプリケーションに触れていなくても(つまり、アイドル状態)、数分後に来てデータを参照すると、同じエラーが発生します
Exception in getSubjects() :org.hibernate.exception.JDBCConnectionException: could not execute query
。
そして、件名の列に空白のデータが表示され、次のクリックでゆっくりとすべてのデータが失われ、休止状態がすべてのリストに対して同じエラーをスローします。
問題: DAO がリストを返す場合とリストを返さない場合がある
この理由は何でしょうか?この問題を解決するために私を助けてください。私は多くの時間を費やしてこのアプリケーションを作成しました。
My SubjectdetailsDAO.java のメソッド (上記のエラーが発生するメソッド)
` public List getSubjects(String subjectname)
{
List list=null;
try{
session = sessionFactory.openSession();
list=session.createCriteria(Subjectdetails.class).add(Expression.like("subjectName", "%" + subjectname + "%")).list();
}catch (Exception e) {
System.out.println("Exception in getSubjects() :" + e);
} finally {
try {
session.flush();
session.close();
} catch (Exception e) {
System.out.println("Exception In getSubjects Resource closing :" + e);
}
}
return list;
} `
HibernateUtil.java
` package v.esoft.connection; import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}`
hibernate.cfg.xml
` <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="vbsofwaresession">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">ashutosh</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/vbsoftware</property>
<property name="hibernate.connection.username">ashutosh</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
<mapping class="v.esoft.pojos.Themes" />
<mapping class="v.esoft.pojos.Usertype" />
<mapping class="v.esoft.pojos.Login" />
<mapping class="v.esoft.pojos.Coverdetails" />
<mapping class="v.esoft.pojos.Editiondetails" />
<mapping class="v.esoft.pojos.Bookdetails" />
<mapping class="v.esoft.pojos.Soldbookdetails" />
<mapping class="v.esoft.pojos.Languagedetails" />
<mapping class="v.esoft.pojos.Locationdetails" />
<mapping class="v.esoft.pojos.Subjectdetails" />
</session-factory>
</hibernate-configuration>
`