可传递语句在传递*作为参数时花费大量时间

时间:2012-12-06 14:12:14

标签: java hibernate stored-procedures jdbc callable-statement

我有以下代码。当我传递像“firstname lastname”这样的普通字符串时。存储过程很快就会返回。但是当我用名字*来称呼它时,需要花费很多时间。当我使用firstname *执行存储过程时,它不会超过2秒。这是下面的代码

public List<NameBean> getNameReportJson(final int numberOfResults,
        final String searchValue, final String category, Connection con)
        throws SQLException {

    final List<NameBean> beanList = new ArrayList();
    List<String> resultStrs = new ArrayList<String>();
    final String nameReportStoreProcedure = "{call [sp_SENTRY_INFORMATION_REPORT_NAME_REPORT] (?,?,?,?,?)}";

    Session session = (Session) em.getDelegate();
    StringBuffer sb = new StringBuffer();

    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            CallableStatement cstmt = connection
                    .prepareCall(nameReportStoreProcedure);
            ResultSet rs = null;

            cstmt.setInt("numberOfResults", numberOfResults);
            cstmt.setString("SearchValue", searchValue);
            cstmt.setString("Category", category);
            cstmt.setDate("startDate", null);
            cstmt.setDate("endDate", null);

            rs = cstmt.executeQuery();
            logger.debug("CeReport the ResultSet is: " + rs);
            while (rs.next()) {
                NameBean nameBean = new NameBean();
                String oriName = rs.getString("ORIGINAL NAME");
                String standName = rs.getString("STANDARDIZED NAME");
                String groupName = rs.getString("GROUP NAME");
                long groupId = rs.getLong("GROUP ID");
                nameBean.setOriginalName(oriName);
                nameBean.setStandardName(standName);
                nameBean.setGroupName(groupName);
                nameBean.setGroupID(groupId);
                nameBean.setEntityID(rs.getString("ENTITY ID"));
                beanList.add(nameBean);
            }

        }

    });

    return beanList;
}

1 个答案:

答案 0 :(得分:0)

找到原因。当存储过程同时发出请求时,就会出现此问题。罪魁祸首是声明

Session session = (Session) em.getDelegate();

所以我要做的就是为每个请求创建一个新的EntityManager实例,然后使用它来获取会话。由于会话由多个线程共享,因此它锁定了表,从而导致了问题。我希望这有助于其他人