【问题标题】:Mule Convert payload string to CLOB for Oracle DB InsertMule 将有效负载字符串转换为 CLOB 以进行 Oracle DB 插入
【发布时间】:2019-06-23 22:22:23
【问题描述】:

问题的基本要点是如何获取字符串有效负载并将其转换为 CLOB 用于 oracle <db:insert ... />

我需要在 oracle 数据库中写入和读取 CLOB 数据。将 String 转换为 CLOB 的典型方法是执行db.getConnection().createClob() 之类的操作,然后将数据设置到这个创建的 CLOB 中。在 MULE 中,这似乎不是一个合法的选择,因为我不会(或至少不平凡)有权访问连接对象。 (也许我可以通过创建新连接并在变压器中进行转换来做一些 hacky 的事情?)

回顾文档和以前的版本,从 V3.5.x 开始,似乎有一个 <jdbc:connector .../> 的旧属性可以处理数据库中的单个行并为这些行提供自定义处理程序。这个 jdbc 连接器已被弃用,我希望它会在未来的版本中被删除。

那么这是如何工作的呢?骡子打算怎么做? 我如何将 String 转换为 CLOB 的 <db:insert .../>

以下是重现问题的示例流程。

<db:oracle-config name="Oracle" host="localhost" port="1521" instance="testIns" user="myUser" password="myPass" doc:name="Oracle Configuration" />
<flow name="databaseInsertFlow">
    <file:inbound-endpoint path="C:\test\input" responseTimeout="10000" doc:name="File"/>
    <file:file-to-string-transformer doc:name="File to String"/>
    <db:insert config-ref="Oracle_Configuration" doc:name="Database">
        <!-- create table tblTest (cdata clob) -->
        <db:parameterized-query><![CDATA[insert into tblTest (cdata) values (#[payload])]]></db:parameterized-query>
    </db:insert>
</flow>

更新: 对该问题的进一步审查表明,问题在于使用 ojdbc7.jar 驱动程序。恢复到 ojdbc6.jar 使用上述流程解决了问题。

【问题讨论】:

    标签: mule mule-studio


    【解决方案1】:

    我确实找到了一种可行的插入解决方案*,但它是一种可怕而丑陋的 hack。

    此解决方案是忽略 mule 提供的数据库连接器并自行开发。

    以下是所需的配置:

    <spring:beans>
      <spring:bean id="dbInserter" scope="prototype" class="kansas.InsertPayloadClob">
        <spring:property name="dbConnection">
            <spring:ref local="Oracle_Configuration"/>
        </spring:property>
      </spring:bean>
    </spring:beans>
    <flow name="databaseInsertFlow">
           <file:inbound-endpoint path="c:\test\input" responseTimeout="10000" doc:name="File"/>
           <file:file-to-string-transformer doc:name="File to String"/>
           <component doc:name="Java">
               <spring-object bean="dbInserter"/>
        </component>
       </flow>
    

    和一个简单的类来实际执行数据库插入

    public class InsertPayloadClob implements Callable { 
    
    private StaticDbConfigResolver dbConnection;
    
    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        String src = eventContext.getMessageAsString();
        DbConfig config = dbConnection.resolve(null);
        try {
            DbConnection conn = config.getConnectionFactory().createConnection(TransactionalAction.JOIN_IF_POSSIBLE);
            Clob clob = conn.createClob();
            clob.setString(1, src);
            try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO tblClobTest (cdata) values(?)")) {
                stmt.setClob(1, clob);
                stmt.executeUpdate();
              }
            return 1; 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    public void setDbConnection(StaticDbConfigResolver o){
        this.dbConnection = o;
    }
    

    【讨论】:

      【解决方案2】:

      用 CLOB 插入 SQL

      将CLOB插入数据库表可以通过以下配置完成:

      <db:insert config-ref="databaseConfiguration" doc:name="Database">
          <db:parameterized-query>
                  <![CDATA[INSERT INTO T_IMPORT_FILE (CONTENT) VALUES (:content)]]>
          </db:parameterized-query>
          <db:in-param name="content" type="CLOB" value="#[payload]" />
      </db:insert>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-27
        相关资源
        最近更新 更多