Results 1 to 7 of 7

Thread: CLOB Support for Oracle stored procedure

  1. #1
    Join Date
    Aug 2004
    Location
    Duesseldorf, Germany
    Posts
    1,210

    Default CLOB Support for Oracle stored procedure

    Hallo everybody,

    I'm currently struggling with writing an clob field in oracle 8 database using build in stored procedure support of spring jdbc package.

    I have the following class:
    Code:
    private static class InsertTransaction extends StoredProcedure {
    
            public InsertTransaction(DataSource ds) {
                super(ds, ECUTransactionDAO.INSERT_PROCEDURE);
                declareParameter(new SqlParameter("REQUEST", Types.CLOB));
                compile();
            }
    
            public Map execute(String request) {
                HashMap map = new HashMap();
                map.put("REQUEST", request);
     
                return super.execute(map);
            }
        }
    Calling the execute method gives me a
    Code:
    java.lang.ClassCastException
    	at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:3069)
    	at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:3107)
    	at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:107)
    	at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:62)
    	at org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:193)
    	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:581)
    	at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:610)
    	at org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:101)
    which I is quite obvious because I try to write a String in an CLOB field.
    But what is the correct way getting a huge string in an clob field using an
    stored procedure?

    Any hints or pointers to examples?

    Thanks

    Christian Dupuis

  2. #2
    Join Date
    Aug 2004
    Posts
    1,110

    Default

    I have not used any of this against an Oracle 8 database but it does work with 8i/9i/10g. If you use the latest 10g JDBC driver then the following code should be sufficient:
    Code:
    	map.put("REQUEST", new SqlLobValue(request));
    If you are using the 9i JDBC driver then you need to use an OracleLobHandler like this (this also works with the 10g driver):
    Code:
    	OracleLobHandler lh = new OracleLobHandler();
    	map.put("REQUEST", new SqlLobValue(request, lh));
    If you are running with a connection pool that wraps the physical Oracle connection then you might need a NativeJdbcExtractor implementation. here is an example that works for Apache DBCP
    Code:
    	OracleLobHandler lh = new OracleLobHandler();
    	lh.setNativeJdbcExtractor(new SimpleNativeJdbcExtractor());
    	map.put("REQUEST", new SqlLobValue(request, lh));
    There are some other implementations provided as well:
    • JBossNativeJdbcExtractor.java
      WebLogicNativeJdbcExtractor.java
      WebSphereNativeJdbcExtractor.java
      XAPoolNativeJdbcExtractor.java
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Aug 2004
    Location
    Duesseldorf, Germany
    Posts
    1,210

    Default

    Thanks for your help.

    After some additional struggling, I figured out how to use your examples within Weblogic 8.1 SP2 (Oracle 9.1i JDBC Driver).

    Declaring an CLOB input parameter on my stored procedure and the following code works just fine within the Weblogic Container.

    Code:
    package clob;
    
    import java.sql.Types;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.sql.DataSource;
    
    import org.springframework.context.ApplicationContextException;
    import org.springframework.jdbc.core.SqlParameter;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    import org.springframework.jdbc.core.support.SqlLobValue;
    import org.springframework.jdbc.object.StoredProcedure;
    import org.springframework.jdbc.support.lob.OracleLobHandler;
    import org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor;
    
    public class ClobDao
            extends JdbcDaoSupport {
    
        private InsertTransaction insert;
    
        private static final String INSERT_PROCEDURE = "TEST_CLOB";
        
        protected void initDao() throws ApplicationContextException {
            insert = new InsertTransaction(getDataSource());
        }
    
        public void set(String status) {
            this.insert.execute(status);
        }
    
        private static class InsertTransaction
                extends StoredProcedure {
    
            public InsertTransaction(DataSource ds) {
                super(ds, ClobDao.INSERT_PROCEDURE);
                declareParameter(new SqlParameter("CLOBTEST_", Types.CLOB));
                compile();
            }
    
            public Map execute(String status) {
                try {
                    HashMap map = new HashMap();
                    OracleLobHandler lh = new OracleLobHandler();
                    lh.setNativeJdbcExtractor(new WebLogicNativeJdbcExtractor());
    
                    map.put("CLOBTEST_", new SqlLobValue(status, lh));
    
                    return super.execute(map);
                }
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return new HashMap();
            }
        }
    }
    Thanks again for your great support.

    Greetings Christian

    P.S. The above code is only for testing purpose. It is not intended for production use. Thats why there are catch(Exception e) and so on.

  4. #4
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    15

    Default

    Just thought that I'd mention that I found this info really helpful. I've had loads of issues with using CLOBs in the past due to connection pooling and it took me no more than 20 mins to get them working using CLOBs.

  5. #5

    Default Cannot Free BLOB with OracleLobHandler

    This post has been very helpful. However I am facing a major issue with the OracleLobHandler. When I use it along with the Spring Stored Procedure, I am able to insert into the table BLOBs. However when I put it in a loop and try to insert into tables, something like 10 BLOBs, one after another, it burst at the 4th BLOB saying that cannot free BLOB. But it works fine for like 1,2 or even 3 blobs, but burst on the 4th and more. However this works absolutely fine when using the jdbcTemplate directly to insert into the table. Has anyone faced the same issue.

    Thanks,
    Franklin

  6. #6
    Join Date
    Nov 2004
    Location
    Ottawa, Canada
    Posts
    7

    Default Oracle LOB's

    I found useful the chapter 6 of the book Building Spring 2 Enterprise Applications (Apress) covering the database access using JDBC templates.

  7. #7

    Default Code Snippet of BLOB

    Thanks radegast .
    If you could send me a sample content from this chapter I could check it out. Else if you could try out the same in a loop with the LOBs it would be really helpful.



    Thanks,
    Franklin.

Similar Threads

  1. Replies: 7
    Last Post: Jan 7th, 2013, 02:58 PM
  2. Replies: 5
    Last Post: Oct 1st, 2010, 08:00 AM
  3. Replies: 0
    Last Post: May 11th, 2005, 06:11 AM
  4. Replies: 1
    Last Post: Nov 25th, 2004, 09:33 PM
  5. Oracle stored procedure support?
    By nilesh in forum Data
    Replies: 8
    Last Post: Aug 12th, 2004, 10:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •