0

I have a stored procedure, which does several updates/inserts. I call the execution of it using Hibernate from java client. I want this piece of code to raise an exception in java in case if procedure will raise an error (if one of the statements will fail because of constrain t violation for example). Quite straightforward, isn't it?

The java code looks like this:

getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session s) throws HibernateException, SQLException {
            SQLQuery query = s.createSQLQuery("exec myProc :date");                             
            query.setTimestamp("date", new Timestamp(valDate.getTime()));
            try {
                query.executeUpdate();
            } catch (HibernateException e) {  ...  } }

in the procedure I'm inserting this statement to imitate error's happening.

raiserror('this is bad', 11, 1);

It works ok if this is the first statement of procedure, so if I call it from mgmt studio the output looks like this:

Msg 50000, Level 11, State 1, Procedure upd_position_list_hist, Line 85 this is bad

Java code catches the exception, everything is ok.

But, if I place the error raising statement in the middle of the proc - after it does some updates/inserts - so that output will looks like :

(0 row(s) affected) (1 row(s) affected) (1 row(s) affected) Msg 50000, Level 11, State 1, Procedure upd_position_list_hist, Line 85 this is bad

  • this stops throwing an exception in java side. Code finished the executing, and no any HibernateException is thrown. What could be the issue and how can I solve it??
4

1 に答える 1

1

set nocount onhibernateはおそらく最初の結果の後で結果の読み取りを停止するため、これを修正する必要があります。単一のクエリから複数の結果を期待することはありません。

しかし、procを実行するために、そもそもなぜ休止状態を使用するのでしょうか。

于 2013-03-12T12:09:59.917 に答える