sqlexception

c9qzyr3d  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(366)

我正在尝试重构我的springboot应用程序,并使用xmltype代替java.sql.sqlxml,因为一些与xmltype相关的方法已经被弃用了,我想重写它。
我在文档中发现,我应该-而不是不赞成:

Document doc = xmltype.getDocument();

用途:

DOMSource domSource = sqlxml.getSource (DOMSource.class);
Document document = (Document) domSource.getNode();

但我有个问题。当我尝试这样做时,我得到了一个例外。
以下是我的代码片段:

String xmlInStr;  
            XMLType xmlIn = null;  
            XMLType xmlOut = null;  
            SQLXML sqlxmlIn = null;  
            SQLXML sqlxmlOut = null;  
            OracleConnection conn = null;  
            Connection hikariConn = null;  
    (...)  
            try {  
                hikariConn = jdbcTemplate.getDataSource().getConnection();  
                sqlxmlIn = hikariConn.createSQLXML();  
                sqlxmlIn.setString(xmlInStr);  
                logger.debug("Input xml has been set.");  
                // input params  
                SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)  

                    .withFunctionName("getQuestionnaireByType")  
                    .withReturnValue()  
                    .withoutProcedureColumnMetaDataAccess()  
                    .declareParameters(  
                            new SqlOutParameter("RETURN",OracleTypes.SQLXML),  
                            new SqlParameter("vLang",OracleTypes.VARCHAR),  
                            new SqlParameter("vRefId",OracleTypes.VARCHAR),  
                            new SqlParameter("vSrcId",OracleTypes.VARCHAR),  
                            new SqlParameter("vUserId",OracleTypes.VARCHAR),  
                            new SqlParameter("vQueType",OracleTypes.VARCHAR),  
                            new SqlParameter("vXmlDataIn",OracleTypes.SQLXML)  
                    )  
                ;  
                jdbcCall.setAccessCallParameterMetaData(false);  
                jdbcCall.setReturnValueRequired(true);  
                jdbcCall.withSchemaName("atr_adap");  

                SqlParameterSource in = new MapSqlParameterSource()  
                        .addValue("vLang", rhData.getLocale().getLanguage())  
                        .addValue("vRefId", rhData.getRequestId())  
                        .addValue("vSrcId", rhData.getSrcId())  
                        .addValue("vUserId", rhData.getUserId())  
                        .addValue("vQueType", queType)  
                        .addValue("vXmlDataIn", sqlxmlIn);  

                // calling db (with XMLType)  
                xmlOut = jdbcCall.executeFunction(XMLType.class, in);  

                logger.debug("Stored Procedure {} executed (XMLType)", jdbcCall.getProcedureName());  
                String xmlOutStr = null;  
                xmlOutStr = xmlOut.getString();  
                logger.info("xml out (XMLType)\r\n{}", xmlOutStr);  
                doc = xmlOut.getDocument(); // everything ok (but getDocument deprecated ...)  

                // calling db (with SQLXML)  
                sqlxmlOut = jdbcCall.executeFunction(java.sql.SQLXML.class, in);  
                logger.debug("Stored Procedure {} executed (SQLXML)", jdbcCall.getProcedureName());  
                xmlOutStr = sqlxmlOut.getString();  
                logger.info("xml out (SQLXML)\r\n{}", xmlOutStr); // everything ok  
                DOMSource domSource = sqlxmlOut.getSource(DOMSource.class); // the following exception is throwing from here  
                doc = (Document) domSource.getNode();   
(...)
java.sql.SQLException: Attempt to read a SQLXML that is not readable. 
    at oracle.xdb.XMLType.getSource(XMLType.java:5159)
    at my.package.JdbcQuestionnairesRepository.findByType(JdbcQuestionnairesRepository.java:239)
    at my.package.JdbcQuestionnairesRepository$$FastClassBySpringCGLIB$$a9555145.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at my.package.JdbcQuestionnairesRepository$$EnhancerBySpringCGLIB$$a184900c.findByType(<generated>)
    at my.package.QuestionnairesService.getQuestionnaireByType(QuestionnairesService.java:48)
(...)

我做错了什么?
致以最诚挚的问候,
科利

lf5gs5x2

lf5gs5x21#

问题似乎出在线路上 xmlOutStr = sqlxmlOut.getString(); . 删除这一行,以及写入 xmlOutStr 你的代码应该能正常工作。
jdbc sqlxml接口的javadoc还提到了以下内容:
一旦free()或任何读取API被调用,状态就会从可读变为不可读:getbinarystream()、getcharacterstream()、getsource()和getstring()。
我说不出原因 XMLType.getDocument() 即使在 .getString() 被称为。

相关问题