hibernate中buildSessionFactory极其慢。 本帖最后由 goleftgoright 于 2010-07-31 20:22:53 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 额 。。我是初学者 也不知道为什么你的会慢。。我用hibernate没出现过慢的情况。。不过我有点奇怪。。你为啥要从session里去拿Connection对象。。 用hibernate不是用的hql语句或QBC来操作么。。底层数据库相关的都封装好了。。 可能是你sessionFactory里面的配置,用连接池试下 因为Configuration cfg = new Configuration();cfg.configure();sessionFactory = cfg.buildSessionFactory();这几条语句执行的时候,每次都会去解析全部的XML文件,而解析XML比较费时间 每一个Session中都封装里一个Connection对象~可以用session.getConnection()方法得到Connection对象~然后就可以执行SQL了~只是这种方法会造成移植性的影响 底层数据库相关我是用jdbc写的,所以必须自己控制connection. import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class JdbcUtil { //ThreadLocal虽然叫本地线程、但并不是一个线程类 //(static)静态类型的ThreadLocal类型变量、在一个线程访问共享内存区域时,给你创建一个当前内存空间的副本、 //建立副本不会影响、其他线程的事务处理 private static ThreadLocal<Connection> threadConnection = new ThreadLocal<Connection>(); /** * 连接SQLServer数据库 * @return */ public static Connection getMsConnection(){ Connection conn = null; String url = null; try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=assetdb;User=root;Password=123"; conn = DriverManager.getConnection(url); if(conn == null){ throw new SQLException("can't connect SQlServler! "); } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(SQLException e2){ e2.printStackTrace(); } return conn; } /** * 连接mySQL数据库 */ public static Connection getMysqlConnection(){ Connection conn = null; String url = null; try{ Class.forName("com.mysql.jdbc.Driver"); url = "jdbc:mysql://localhost:3306/itemdb"; conn = DriverManager.getConnection(url, "root", "123"); if(conn == null){ throw new SQLException("can't connect mySQL..."); } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(SQLException e2){ e2.printStackTrace(); } return conn; } /** * 获取当前连接(Connection)、当连接失败时重新连接 * @return */ public static Connection getCurrentConnection() throws SQLException{ //获得一个ThreadLocal的一个副本(仓位): Connection conn = (Connection)threadConnection.get(); //判断当前连接是否为空:如果为空则获得一个Connection、并将Connection放入ThreadLocal中 if(conn == null){ System.out.println("Open a new connection for this threadConnection..."); conn = getMysqlConnection(); threadConnection.set(conn); } return conn; } /** * 获得当前连接,判断事务是否启动 * @param isTransaction * @return * @throws SQLException */ public static Connection getCurrentConnection(boolean isTransaction) throws SQLException{ Connection conn = getCurrentConnection(); //启动事务 if(isTransaction){ conn.setAutoCommit(false); } return conn; } /** * 关闭当前连接(Connection),//并提交事务 */ public static void closeCurrentConnection(){ try{ Connection conn = (Connection)threadConnection.get(); threadConnection.set(null); conn.close(); } catch(SQLException e){ e.printStackTrace(); } } /** * 关闭指定数据库的连接 * @param conn * @throws SQLException */ public static void closeConnection(Connection conn){ try{ if(conn != null){ conn.close(); } } catch(SQLException e){ e.printStackTrace(); } } /*********************************** ******* Transaction事务处理操作方法 ***********************************/ /** * 开始一个新的事务 */ public static void beginTransaction(){ try{ getCurrentConnection().setAutoCommit(false); } catch(SQLException e){ e.printStackTrace(); } } /** * 提交数据库事务 */ public static void commitTransaction(){ try{ getCurrentConnection().commit(); } catch(SQLException e){ e.printStackTrace(); } } /** * 回滚当前事务 */ public static void rollbackTransaction(){ try{ getCurrentConnection().rollback(); } catch(SQLException e){ e.printStackTrace(); } } } import java.lang.reflect.InvocationTargetException;import java.sql.Connection;import java.sql.ParameterMetaData;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.commons.beanutils.BeanUtils;import com.qrsx.appcam.model.User;public class BaseDAO { protected Connection conn = null; protected PreparedStatement ps = null; //protected ResultSet rs = null; //定义一个构造方法用来连接数据库,并启动事务 public BaseDAO() { try { //conn = JdbcUtil.getCurrentConnection(true); //直接获取Connection, 无需打开事务 this.conn = JdbcUtil.getCurrentConnection(); } catch (Exception e) { e.printStackTrace(); } } /** * 用户自定义Connection */ public void setConnection(Connection con) { this.conn = conn; } /** * 对插入和更新语句, 实现其预备语句的自动组装。//删除和检索语句待定??? * 自动组装预备语句 Object实体对象 * @throws InvocationTargetException * @throws IllegalAccessException //PreparedStatement */ public void processStatement(String sql, PreparedStatement ps, Object object) throws Exception { ps = conn.prepareStatement(sql); String[] str = null; //需要解析sql语句,取出定义的相应字段, //String sql = " Insert Into `user`(name, age, telephone) Value(?, ?, ?)"; //String sql = "Update user set name=?, age=?, telephone=? Where id=?"; //String sql = "Select//Update" //判断sql是创建还是更新 //^i:匹配输入字符串的开始位置 //创建语句 if ( sql.matches("^\\si.*") || sql.matches("^\\sI.*") ) { String[] s = sql.split("\\("); String string = s[1]; s = string.split("\\)"); string = s[0]; s = string.split("\\,"); str = s; } else { //sql.mathes("^u.*") //更新语句 String[] s = sql.split("\\="); for (int i = 0; i < s.length - 1; i++) { String string = s[i]; String[] s1 = string.split(" "); String string2 = s1[s1.length - 1]; s[i] = string2; } str = s; } //实例化一个Map集合 HashMap map = new HashMap(); //把map填充 map = (HashMap) BeanUtils.describe(object); //获取PreparedStatement对象 ParameterMetaData rsmd = ps.getParameterMetaData(); //rsmd.getParameterCount():获取 PreparedStatement 对象中的参数的数量, //此 ParameterMetaData 对象包含了该对象的信息。 for (int i = 0; i < rsmd.getParameterCount(); i++) { ps.setObject(i + 1, map.get(str[i])); } ps.executeUpdate(); } /** * 对插入和更新语句, 实现其预备语句的自动组装。//删除和检索语句待定??? * 自动组装预备语句 Object实体对象 * @throws InvocationTargetException * @throws IllegalAccessException //PreparedStatement */ public PreparedStatement processStatement2(String sql, PreparedStatement ps, Object object) throws Exception { String[] array = null; //^i:匹配输入字符串的开始 //String sql = " Insert Into `user`(name, age, telephone) Value(?, ?, ?)"; //String sql = "Update user set name=?, age=?, telephone=? Where id=?"; if (sql.matches("^\\s*i.*") || sql.matches("^\\s*I.*")) { //插入语句、i前匹配0个或多个空格 String[] s1 = sql.split("\\("); String s2 = s1[1]; s1 = s2.split("\\)"); s2 = s1[0]; array = s2.split(","); } else if( sql.matches("^\\s*U.*") || sql.matches("^\\s*u.*") ){ //更新语句、u前匹配0个或多个空格 String[] s = sql.split("\\="); array = new String[s.length-1]; for (int i = 0; i < s.length - 1; i++) { String string = s[i]; String[] s1 = string.split("\\s+"); //匹配一个或多个空格 array[i] = s1[s1.length - 1]; } } else { //删除、检索略 array = null; } //实例化一个Map集合 HashMap map = new HashMap(); //组装map集合:以实体对象的属性名为键、以实体对象的属性值为值组装map map = (HashMap) BeanUtils.describe(object); //获取PreparedStatement对象 ps = conn.prepareStatement(sql); ParameterMetaData rsmd = ps.getParameterMetaData(); //ParameterMetaData 对象包含了表对象信息 //从数组中获得键所对应的值时,需要去掉空格 for (int i = 1; i < rsmd.getParameterCount()+1; i++) { if( array != null){ ps.setObject(i, map.get(array[i-1].trim())); } else{ return this.ps; } } return ps; } /** * 实现用结果集自动组装实体对象、返回List * @param object 实体对象 * @throws Exception */ public List processResultSet( Object obj, ResultSet rs ) throws Exception { //需要解析sql语句,取出定义的相应字段, //通过sql语句中的字段,确定实体对象的属性,然后对实体对象进行组装 List list = new ArrayList(); //获取此ResultSet对象的列的编号、类型和属性。 ResultSetMetaData rsmd = rs.getMetaData(); Map<String, Object> map = null; while (rs.next()) { map = new HashMap<String, Object>(); obj = new Object(); //结果集下标从1开始 for (int i = 1; i < rsmd.getColumnCount(); i++) { //获取当前列的列名 String columnName = rsmd.getColumnName(i); String columnValue = rs.getString(columnName); /*String columnClassName = rsmd.getColumnClassName(i).substring(10); if ("String".equals(columnClassName)) { String columnValue = rs.getString(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Integer".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Double".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Float".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Long".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Short".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else if ("Byte".equals(columnClassName)) { Integer columnValue = rs.getInt(i); map.put(columnName, columnValue); BeanUtils.populate(entityObj, map); } else { Object columnValue = rs.getObject(i); map.put(columnName, columnValue); }*/ map.put(columnName, columnValue); } BeanUtils.populate(obj, map); list.add(obj); } return list; } /** * 实现用结果集自动组装实体对象、返回List * @param object 实体对象 * @throws Exception */ public Object processResultSet2(Object entity, ResultSet rs) throws Exception { //获取此ResultSet对象的列的编号、类型和属性。 ResultSetMetaData rsmd = rs.getMetaData(); Map map= new HashMap(); for (int i = 1; i < rsmd.getColumnCount()+1; i++) { map.put(rsmd.getColumnName(i), rs.getObject(i)); } BeanUtils.populate(entity, map); return entity; } public void processResultSet3(Object entity, ResultSet rs) throws Exception { //获取此ResultSet对象的列的编号、类型和属性。 ResultSetMetaData rsmd = rs.getMetaData(); Map map= new HashMap(); for (int i = 1; i < rsmd.getColumnCount()+1; i++) { map.put(rsmd.getColumnName(i), rs.getObject(i)); } BeanUtils.populate(entity, map); } } jacob将excel另存为pdf格式,只能转出当前活动的表 关于一个SqlMap中Mysql语句的一个问题 大家支持下 操作Excel的问题 struts 问题标签用户 基于MyEclipse的登录界面的设计 如何实现这个类??初学者提问!! 《Getting Started with JBoss J2EE Applications on JBoss 4.0中文版》下载 struts-config 中,不能编辑Action 新增修改都提示错误 大虾们,指点指点小弟 大家帮忙解决一个方法的实现! Spring 怎样将properties文件的值,注入到Action中
不过我有点奇怪。。你为啥要从session里去拿Connection对象。。 用hibernate不是用的hql语句或QBC来操作么。。底层数据库相关的都封装好了。。
Configuration cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
这几条语句执行的时候,每次都会去解析全部的XML文件,而解析XML比较费时间
底层数据库相关我是用jdbc写的,所以必须自己控制connection.
import java.sql.DriverManager;
import java.sql.SQLException;public class JdbcUtil { //ThreadLocal虽然叫本地线程、但并不是一个线程类
//(static)静态类型的ThreadLocal类型变量、在一个线程访问共享内存区域时,给你创建一个当前内存空间的副本、
//建立副本不会影响、其他线程的事务处理
private static ThreadLocal<Connection> threadConnection = new ThreadLocal<Connection>();
/**
* 连接SQLServer数据库
* @return
*/
public static Connection getMsConnection(){
Connection conn = null;
String url = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=assetdb;User=root;Password=123";
conn = DriverManager.getConnection(url);
if(conn == null){
throw new SQLException("can't connect SQlServler! ");
}
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e2){
e2.printStackTrace();
}
return conn;
}
/**
* 连接mySQL数据库
*/
public static Connection getMysqlConnection(){
Connection conn = null;
String url = null;
try{
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://localhost:3306/itemdb";
conn = DriverManager.getConnection(url, "root", "123");
if(conn == null){
throw new SQLException("can't connect mySQL...");
}
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e2){
e2.printStackTrace();
}
return conn;
} /**
* 获取当前连接(Connection)、当连接失败时重新连接
* @return
*/
public static Connection getCurrentConnection() throws SQLException{
//获得一个ThreadLocal的一个副本(仓位):
Connection conn = (Connection)threadConnection.get();
//判断当前连接是否为空:如果为空则获得一个Connection、并将Connection放入ThreadLocal中
if(conn == null){
System.out.println("Open a new connection for this threadConnection...");
conn = getMysqlConnection();
threadConnection.set(conn);
}
return conn;
}
/**
* 获得当前连接,判断事务是否启动
* @param isTransaction
* @return
* @throws SQLException
*/
public static Connection getCurrentConnection(boolean isTransaction) throws SQLException{
Connection conn = getCurrentConnection();
//启动事务
if(isTransaction){
conn.setAutoCommit(false);
}
return conn;
}
/**
* 关闭当前连接(Connection),//并提交事务
*/
public static void closeCurrentConnection(){
try{
Connection conn = (Connection)threadConnection.get();
threadConnection.set(null);
conn.close();
} catch(SQLException e){
e.printStackTrace();
}
}
/**
* 关闭指定数据库的连接
* @param conn
* @throws SQLException
*/
public static void closeConnection(Connection conn){
try{
if(conn != null){
conn.close();
}
} catch(SQLException e){
e.printStackTrace();
}
}
/***********************************
******* Transaction事务处理操作方法
***********************************/
/**
* 开始一个新的事务
*/
public static void beginTransaction(){
try{
getCurrentConnection().setAutoCommit(false);
} catch(SQLException e){
e.printStackTrace();
}
}
/**
* 提交数据库事务
*/
public static void commitTransaction(){
try{
getCurrentConnection().commit();
} catch(SQLException e){
e.printStackTrace();
}
}
/**
* 回滚当前事务
*/
public static void rollbackTransaction(){
try{
getCurrentConnection().rollback();
} catch(SQLException e){
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;import org.apache.commons.beanutils.BeanUtils;import com.qrsx.appcam.model.User;public class BaseDAO { protected Connection conn = null;
protected PreparedStatement ps = null;
//protected ResultSet rs = null; //定义一个构造方法用来连接数据库,并启动事务
public BaseDAO() {
try {
//conn = JdbcUtil.getCurrentConnection(true);
//直接获取Connection, 无需打开事务
this.conn = JdbcUtil.getCurrentConnection();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 用户自定义Connection
*/
public void setConnection(Connection con) {
this.conn = conn;
}
/**
* 对插入和更新语句, 实现其预备语句的自动组装。//删除和检索语句待定???
* 自动组装预备语句 Object实体对象
* @throws InvocationTargetException
* @throws IllegalAccessException //PreparedStatement
*/
public void processStatement(String sql, PreparedStatement ps, Object object)
throws Exception {
ps = conn.prepareStatement(sql);
String[] str = null; //需要解析sql语句,取出定义的相应字段,
//String sql = " Insert Into `user`(name, age, telephone) Value(?, ?, ?)";
//String sql = "Update user set name=?, age=?, telephone=? Where id=?";
//String sql = "Select//Update"
//判断sql是创建还是更新
//^i:匹配输入字符串的开始位置 //创建语句
if ( sql.matches("^\\si.*") || sql.matches("^\\sI.*") ) {
String[] s = sql.split("\\(");
String string = s[1];
s = string.split("\\)");
string = s[0];
s = string.split("\\,");
str = s;
} else { //sql.mathes("^u.*") //更新语句
String[] s = sql.split("\\=");
for (int i = 0; i < s.length - 1; i++) {
String string = s[i];
String[] s1 = string.split(" ");
String string2 = s1[s1.length - 1];
s[i] = string2;
}
str = s;
} //实例化一个Map集合
HashMap map = new HashMap(); //把map填充
map = (HashMap) BeanUtils.describe(object); //获取PreparedStatement对象
ParameterMetaData rsmd = ps.getParameterMetaData();
//rsmd.getParameterCount():获取 PreparedStatement 对象中的参数的数量,
//此 ParameterMetaData 对象包含了该对象的信息。
for (int i = 0; i < rsmd.getParameterCount(); i++) {
ps.setObject(i + 1, map.get(str[i]));
}
ps.executeUpdate();
}
/**
* 对插入和更新语句, 实现其预备语句的自动组装。//删除和检索语句待定???
* 自动组装预备语句 Object实体对象
* @throws InvocationTargetException
* @throws IllegalAccessException //PreparedStatement
*/
public PreparedStatement processStatement2(String sql, PreparedStatement ps, Object object)
throws Exception {
String[] array = null;
//^i:匹配输入字符串的开始
//String sql = " Insert Into `user`(name, age, telephone) Value(?, ?, ?)";
//String sql = "Update user set name=?, age=?, telephone=? Where id=?";
if (sql.matches("^\\s*i.*") || sql.matches("^\\s*I.*")) { //插入语句、i前匹配0个或多个空格
String[] s1 = sql.split("\\(");
String s2 = s1[1];
s1 = s2.split("\\)");
s2 = s1[0];
array = s2.split(",");
} else if( sql.matches("^\\s*U.*") || sql.matches("^\\s*u.*") ){ //更新语句、u前匹配0个或多个空格
String[] s = sql.split("\\=");
array = new String[s.length-1];
for (int i = 0; i < s.length - 1; i++) {
String string = s[i];
String[] s1 = string.split("\\s+"); //匹配一个或多个空格
array[i] = s1[s1.length - 1];
}
} else { //删除、检索略
array = null;
}
//实例化一个Map集合
HashMap map = new HashMap(); //组装map集合:以实体对象的属性名为键、以实体对象的属性值为值组装map
map = (HashMap) BeanUtils.describe(object);
//获取PreparedStatement对象
ps = conn.prepareStatement(sql);
ParameterMetaData rsmd = ps.getParameterMetaData();
//ParameterMetaData 对象包含了表对象信息
//从数组中获得键所对应的值时,需要去掉空格
for (int i = 1; i < rsmd.getParameterCount()+1; i++) {
if( array != null){
ps.setObject(i, map.get(array[i-1].trim()));
} else{
return this.ps;
}
}
return ps;
}
/**
* 实现用结果集自动组装实体对象、返回List
* @param object 实体对象
* @throws Exception
*/
public List processResultSet( Object obj, ResultSet rs ) throws Exception { //需要解析sql语句,取出定义的相应字段,
//通过sql语句中的字段,确定实体对象的属性,然后对实体对象进行组装
List list = new ArrayList(); //获取此ResultSet对象的列的编号、类型和属性。
ResultSetMetaData rsmd = rs.getMetaData();
Map<String, Object> map = null; while (rs.next()) { map = new HashMap<String, Object>();
obj = new Object();
//结果集下标从1开始
for (int i = 1; i < rsmd.getColumnCount(); i++) { //获取当前列的列名
String columnName = rsmd.getColumnName(i);
String columnValue = rs.getString(columnName);
/*String columnClassName = rsmd.getColumnClassName(i).substring(10);
if ("String".equals(columnClassName)) {
String columnValue = rs.getString(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Integer".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Double".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Float".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Long".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Short".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else if ("Byte".equals(columnClassName)) {
Integer columnValue = rs.getInt(i);
map.put(columnName, columnValue);
BeanUtils.populate(entityObj, map);
} else {
Object columnValue = rs.getObject(i);
map.put(columnName, columnValue);
}*/
map.put(columnName, columnValue);
}
BeanUtils.populate(obj, map);
list.add(obj);
} return list;
} /**
* 实现用结果集自动组装实体对象、返回List
* @param object 实体对象
* @throws Exception
*/
public Object processResultSet2(Object entity, ResultSet rs) throws Exception { //获取此ResultSet对象的列的编号、类型和属性。
ResultSetMetaData rsmd = rs.getMetaData();
Map map= new HashMap(); for (int i = 1; i < rsmd.getColumnCount()+1; i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
BeanUtils.populate(entity, map);
return entity;
}
public void processResultSet3(Object entity, ResultSet rs) throws Exception { //获取此ResultSet对象的列的编号、类型和属性。
ResultSetMetaData rsmd = rs.getMetaData();
Map map= new HashMap(); for (int i = 1; i < rsmd.getColumnCount()+1; i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
BeanUtils.populate(entity, map);
}
}