我正在尝试从数据库中获取一些数据。我正在使用Hibernate框架进行提取,创建会话并获取db值需要超过40秒。我听说通常只用毫秒范围。什么是实际问题。请提出我可能犯的错误。
public Map<String, String> regions() {
Map<String, String> regionMap = new HashMap<String, String>();
Criteria criteria = getTemplate().getSession().createCriteria(LocationMaster.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("name"));
projectionList.add(Projections.property("id"));
criteria.setProjection(projectionList);
ArrayList<Object[]> regionList = (ArrayList<Object[]>) getTemplate().get(criteria);
System.out.println(regionList.get(0));
for (Object[] list : regionList) {
regionMap.put(list[0].toString(), list[1].toString());
System.out.println(list[0]);
System.out.println(list[1]);
}
Map<String, String> map = new TreeMap<String, String>(regionMap);
return map;
}
这是代码而我没有使用DAO。
public Session getSession() {
try {
if (session == null || !session.isOpen()) {
session = factory.openSession();
txn = session.beginTransaction();
}
} catch (JDBCConnectionException e) {
logger.error("JDBC Connection Exception " + e.getStackTrace());
System.out.println("JDBC Connection Exception " + e.getStackTrace());
factory.close();
factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
txn = session.beginTransaction();
}
return session;
}
public HibernateTemplate() {
if (factory == null || !factory.isClosed()) {
System.out.println("hibernate constructor");
factory = new Configuration().configure().buildSessionFactory();
}
}
这是我的getsession()方法。