SELECT INTO command not allowed within multi-statement transaction in Hibernate for Sybase DB

吃可爱长大的小学妹 提交于 2019-12-11 11:14:58

问题


I was new to hibernate and trying to execute an procedure from a Java file using hibernate to a Sybase DB. While i am trying to run the application i am getting an error like below

Stored procedure 'dbo.p_chklist_test' may be run only in unchained transaction mode. The 'SET CHAINED OFF' command will cause the current session to use unchained transaction mode.

I have checked in few forums and set the mode as "Any mode" by running below command. sp_procxmode p_chklist_test, "anymode"

Also i have set the Auto Commit as False in hibernate.

Now i am getting a different error like below

Caused by: org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:198)
    at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1191)
    at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:357)
    at com.lcit_release.server.dao.ReleaseItemDao.searchRecordsNew(ReleaseItemDao.java:198)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy8.searchRecordsNew(Unknown Source)
    at com.lcit_release.server.logic.ReleaseItemLogic.searchExisting(ReleaseItemLogic.java:147)
    at com.lcit_release.server.adapter.ReleaseItemLogicAdapter.search(ReleaseItemLogicAdapter.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
    ... 41 more


**Caused by: com.sybase.jdbc3.jdbc.SybSQLException: SELECT INTO command not allowed within multi-statement transaction.**

    at com.sybase.jdbc3.tds.Tds.a(Unknown Source)
    at com.sybase.jdbc3.tds.Tds.nextResult(Unknown Source)
    at com.sybase.jdbc3.jdbc.ResultGetter.nextResult(Unknown Source)
    at com.sybase.jdbc3.jdbc.SybStatement.nextResult(Unknown Source)
    at com.sybase.jdbc3.jdbc.SybStatement.nextResult(Unknown Source)
    at com.sybase.jdbc3.jdbc.SybStatement.updateLoop(Unknown Source)
    at com.sybase.jdbc3.jdbc.SybStatement.executeUpdate(Unknown Source)
    at com.sybase.jdbc3.jdbc.SybPreparedStatement.executeUpdate(Unknown Source)
    at msjava.tools.db.jdbc3.MSDBPreparedStatementImpl.executeUpdate(MSDBPreparedStatementImpl.java:315)
    at msjava.tools.db.jdbc3.MSDBPreparedStatement.executeUpdate(MSDBPreparedStatement.java:78)
    at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:189)
    ... 62 more

I have check few sites for the error SELECT INTO command not allowed within multi-statement transaction and set the parameter "ServerInitiatedTransactions" as false in the configuration xml

**<ConnectProperties>
    <Property name="ServerInitiatedTransactions">false</Property>
</ConnectProperties>**  

But this even dint resolve the issue and i am getting the same error. Can someone please help me on this.

My Code:

 String sql3 ="exec dbo.p_chklist_test";
         System.out.println("sql 3 is "+sql3);

            Query query = sessionFactory.getCurrentSession().createSQLQuery(sql3);


             sessionFactory.getCurrentSession().connection().setAutoCommit(false);



         query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);


         listRelItem = query.list();

Thanks in Advance!


回答1:


Please check your stored procedure for statements like

SELECT id INTO #a FROM students.

This statement is okay from DB perspective but when executed from a Java Program this won't work and give the above error.

First, define the temporary table

CREATE TABLE #a( id INT )

INSERT INTO #a SELECT id FROM students

The above fix works.




回答2:


I have solved the issue by calling the procedure in the below manner. Hope it might help others to figure it out.

SET CHAINED OFF exec p_qa_existing_items




回答3:


Probably this is because you should "never" try to use "select into" while a transaction(select @@transaction) is open, call it from any platform Java or any other application.

I believe solution given by @swat is pretty much you should use rather than SET CHAINED OFF. There could be impact due to this as in your code as I understand SET CHAINED was ON for some reason. So there might not be any code in SP when there is need to open a transaction but you have not as SET CHAINED is ON.

You can play with this in Sybase -

create proc testproc as select * into #temp from table return go

begin tran exec test_proc commit tran

begin tran exec test_proc rollback tran

(I do not have my Sybase environment with me now so cannot check it for myself. but this could definitely help you understand the cause)




回答4:


I had the same problem using Jboss, and I couldn't change the procedure because it was provided by the client. In my case the problem stopped after configuring the datasource to use

jta="false".


来源:https://stackoverflow.com/questions/21605632/select-into-command-not-allowed-within-multi-statement-transaction-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!