 2008-08-26 10:58
浏览 272
已采纳

hibernate调用存储过程。连接问题

。。。很闷。

现在有个部分需要经常调用存储过程然后访问sqlserver数据库存储过程。

公司用的是hibernate。连接池是用的c3po。

org.hibernate.connection.C3P0ConnectionProvider
20
2
50000
100
3000
2
false
这是配置文件

public PreCallout getcalloutrecord(){
PreCallout pc=new PreCallout();
String sql="{call dt_getcalloutrecord}";
Connection conn=null;
CallableStatement cst=null;
ResultSet rs=null;

    try {
        conn=getSession().connection();
        conn.setAutoCommit(true);
        cst=conn.prepareCall(sql);
        rs=cst.executeQuery();
        if(rs.next()){
            pc.setCallee(rs.getString("callee"));
            pc.setCid(rs.getInt("cid"));
            pc.setCaller(rs.getString("caller"));
        }

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        pc.setCid(-1);
        return pc;
    }

    return pc;
}

这是调用存储过程的方法。

现在是这样一个问题。执行多次以后是为什么会执行很慢?

如果您说是连接没关。请问到底关哪些?详细点?

我试过finally{ rs关闭,cst关闭,conn关闭}。。

到底需要关闭吗。不是连接池吗。。

反正我是头大了。。关也慢。不关也慢。。

头大啊。。还有是这样慢的。。慢到执行成功一次。然后继续刷新就很快。

重新开启浏览器的话。又很慢很慢。。help!

[b]问题补充:[/b]
慢的原因原来是其他表的事务将表锁住了。
。。
关了也一样慢。
我写了啊。昏!

  • 写回答

1条回答 默认 最新

  • iteye_2718 2008-08-26 18:30
    关注

    我把你的程序改了一下,我也试过用这种方法调用存储过程
    public PreCallout getcalloutrecord(){
    PreCallout pc=new PreCallout();
    String sql="{call dt_getcalloutrecord}";
    Connection conn=null;
    CallableStatement cst=null;
    ResultSet rs=null;

    try {
    [color=red]Session session=getSession();
    conn=session.connection(); [/color]
    conn.setAutoCommit(true);
    cst=conn.prepareCall(sql);
    rs=cst.executeQuery();
    if(rs.next()){
    pc.setCallee(rs.getString("callee"));
    pc.setCid(rs.getInt("cid"));
    pc.setCaller(rs.getString("caller"));
    }

    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    pc.setCid(-1);
    return pc;
    } finally{
    [color=red] rs.close();
    pc.close();
    session.close();[/color]
    }
    return pc;
    }

    红色部分为我改的,rs和pc是一定要关的,如果你的程序是web应该,你先搞清楚这个web是用什么框架来管理session,如果用ThreadLoad机制在Filter上关,session是不用关的.(就是上面 session.close()这个不要)
    还要你可以打印一下操作时间,看一下是访问数据库的时候慢,还是返回的数据太多倒至的慢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)