0
有些奇怪的事情正在發生在我身上。我有一個項目正在測試我的數據訪問層,好嗎?但我跑了測試,他通過了。後來我發現我的Mysql Diamond沒有運行。可能發生了什麼?即使mysqld沒有啓動,會話似乎也存在
//測試代碼
public class AppTest {
static final Logger log = Logger.getLogger(AppTest.class);
@Test
public void sessionTest() { // Passed
Session s = HibernateUtil.getSessionFactory().openSession();
Assert.assertTrue(s.isOpen());
}
@Test
public void fetchEvent() { // Failed
EventDao edao = new EventDao();
Event e = null;
try {
e = edao.find(1);
} catch (Exception ex) {
log.fatal(ex.getMessage());
}
Assert.assertNotNull(e);
}
@Test
public void fetchAll() { // Failed
EventDao edao = new EventDao();
List<Event> e = null;
try {
e = edao.all(); // I have 6 rows on the Event table
} catch (Exception ex) {
log.fatal(ex.getMessage());
}
Assert.assertEquals(e.size(), 6);
}
}
// HibernateUtil的
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
//的src /主/資源/ 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>
<!-- Connection settings -->
<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/MyDB</property>
<property name="hibernate.connection.username">root</property>
<!-- Class mappings -->
...
<mapping class="br.siseventos.siseventosmaventest.model.Event"/>
...
</session-factory>
// EventDao代碼
public class EventDao extends BaseDao<Event> {
}
// BaseDao代碼
public abstract class BaseDao<T> implements Dao<T> {
// Fields
private Class actualClass;
// Constructor
public BaseDao() {
// Fetching generic class parameter
actualClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
// Getters e Setters
public Class getActualClass() {
return actualClass;
}
public void setActualClass(Class actualClass) {
this.actualClass = actualClass;
}
// Service
public T find(int id) throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = null;
T o = null;
try {
t = session.beginTransaction();
o = (T) session.get(actualClass, new Integer(id));
t.commit();
} catch (Exception e) {
if (t != null) {
try {
t.rollback();
} catch (Exception ex) {
}
}
throw e;
} finally {
if (session != null) {
session.close();
}
}
return o;
}
public List<T> all() throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = null;
List<T> o = null;
try {
t = session.beginTransaction();
o = (List<T>) session.createQuery("from " + getActualClassName()).list();
t.commit();
} catch (Exception e) {
if (t != null) {
try {
t.rollback();
} catch (Exception ex) {
}
}
throw e;
} finally {
if (session != null) {
session.close();
}
}
return o;
}
// Util
public String getActualClassName() {
return getActualClass().getSimpleName();
}
}
AFAIK打開會話並不意味着打開連接。它在第一次使用時被打開 – Firo
@Firo對不起,問這樣的問題!懶加載! –