hibernate session获取Connection

阅读更多
public int executeSql(String[] sql) {
		int key = 0;

		ConnectionProvider cp = ((SessionFactoryImplementor) getSession().getSessionFactory()).getConnectionProvider();
		Connection conn = null;
		PreparedStatement pps = null;
		try {
			conn = cp.getConnection();
			conn.setAutoCommit(false);
			for (int i = 0; i < sql.length; i++) {
				if (sql[i] == null || "".equals(sql[i]))
					break;
				pps = conn.prepareStatement(sql[i]);
				pps.execute();
				pps.close();
				key++;
				if (key % 1000 == 0) {
					conn.commit();					
				}
			}
			conn.commit();
		} catch (Exception e) {
			if (conn != null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return key;
	}

你可能感兴趣的:(hibernate,java,jdbc,批量插入)