2012-09-18 1 views
0

조금 이상해 보입니다. 내 데이터 액세스 레이어를 테스트하고있는 프로젝트가 있습니다. 그러나 나는 시험을 치렀고 그는 지나갔습니다. 나중에 나는 내 MySQL 다이아몬드가 실행되고 있지 않다는 것을 발견했다. 무슨 일이 일어날까요?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(); 
} 

} 연결을 여는 의미하지 않는다 세션을 열어

+0

AFAIK 세션 열기는 연결을 여는 것을 의미하지 않습니다. 그것은 처음 사용시 열립니다 – Firo

+0

@Firo 그런 dork 질문에 대해 유감스럽게 생각합니다. 게으른 로딩! –

답변

1

AFAIK. 처음 사용시 열림

관련 문제