-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: BinaryBlobType problem with c3p0 8.5.1
PostPosted: Tue Jun 07, 2005 9:01 pm 
Newbie

Joined: Mon Dec 13, 2004 6:54 pm
Posts: 2
I'm using user defined BinaryBlobType in my code and never had any problems with it until I recently upgraded to c3p0 version 8.5.1 from c3p0 version 8.4.5. Here is the code for BinaryBlobType

Code:
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;
import java.io.*;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class BinaryBlobType implements UserType
{
  public int[] sqlTypes()
  {
    return new int[] { Types.BLOB };
  }

  public Class returnedClass()
  {
    return byte[].class;
  }

  public boolean equals(Object x, Object y)
  {
    return (x == y)
      || (x != null
        && y != null
        && java.util.Arrays.equals((byte[]) x, (byte[]) y));
  }


  public Object deepCopy(Object value)
  {
    if (value == null) return null;

    byte[] bytes = (byte[]) value;
    byte[] result = new byte[bytes.length];
    System.arraycopy(bytes, 0, result, 0, bytes.length);

    return result;
  }

  public boolean isMutable()
  {
    return true;
  }


  //Modified code
  public void nullSafeSet(PreparedStatement st, Object value, int index)
  throws HibernateException, SQLException
  {


      System.out.println("PreparedStatement is of type: " + st.getClass().getName());
     
      if(st instanceof  com.mchange.v2.sql.filter.FilterPreparedStatement &&
         ((com.mchange.v2.sql.filter.FilterPreparedStatement) st).getInner()     
         instanceof oracle.jdbc.OraclePreparedStatement)
      {
         
          oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(
                st.getConnection().getMetaData().getConnection(), false, oracle.sql.BLOB.DURATION_SESSION);

          //register temporary blob for future cleanup
          HibernateUtil.registerTempBLOB(blob);

          blob.open(oracle.sql.BLOB.MODE_READWRITE);
         
          OutputStream out = blob.getBinaryOutputStream();
         
          try
              {
                  out.write((byte[])value);
                  out.flush();
                  out.close();
              }
          catch(IOException e)
              {
                  throw new SQLException("failed write to blob" + e.getMessage());
              }
         
          blob.close();
         
          ((oracle.jdbc.OraclePreparedStatement)((com.mchange.v2.sql.filter.FilterPreparedStatement)st).getInner()).setBytes(index,
                                                                                                  blob.getBytes(1, (int) blob.length()));
      }

      else if( st instanceof  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement )
               
      {
          com.mchange.v2.c3p0.impl.NewProxyPreparedStatement npc = (com.mchange.v2.c3p0.impl.NewProxyPreparedStatement) st;

         
         
          if( ! ((npc.getConnection().getMetaData().getConnection()) instanceof oracle.jdbc.OracleConnection) )
                throw new HibernateException(" connection not of type OracleConnection");
             

          oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(
           (oracle.jdbc.OracleConnection) ((com.mchange.v2.c3p0.impl.NewProxyPreparedStatement)  st).getConnection().getMetaData().getConnection(), false, oracle.sql.BLOB.DURATION_SESSION);

          blob.open(oracle.sql.BLOB.MODE_READWRITE);
         
          OutputStream out = blob.getBinaryOutputStream();
         
          try
              {
                  out.write((byte[])value);
                  out.flush();
                  out.close();
              }
          catch(IOException e)
              {
                  throw new SQLException("failed write to blob" + e.getMessage());
              }
         
          blob.close();
         
          ((com.mchange.v2.c3p0.impl.NewProxyPreparedStatement) st).setBytes( index, blob.getBytes(1, (int) blob.length()));
         

   

      }
     
      else if (st instanceof oracle.jdbc.OraclePreparedStatement)
          {
              oracle.sql.BLOB blob =
                  oracle.sql.BLOB.createTemporary(st.getConnection(),
                                                  false,
                                                  oracle.sql.BLOB.DURATION_SESSION);

              blob.open(oracle.sql.BLOB.MODE_READWRITE);

              OutputStream out = blob.getBinaryOutputStream();
             
              try
                  {
                      out.write((byte[]) value);
                      out.flush();
                      out.close();
                  }
              catch (IOException e)
                  {
                      throw new SQLException("failed write to blob" + e.getMessage());
                  }
             
              blob.close();
             
              //((oracle.jdbc.OraclePreparedStatement) st).setBLOB( index, blob);
              ((oracle.jdbc.OraclePreparedStatement) st).setBytes( index, blob.getBytes(1, (int) blob.length()));
          }     
      else
          {
              st.setBlob(index, Hibernate.createBlob((byte[]) value));
          }
  }

  //and.. note the null check, oracle drivers return a null blob...
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
     throws HibernateException, SQLException
  {
      final Blob blob = rs.getBlob(names[0]);
      return blob != null?blob.getBytes(1, (int)blob.length()):null;
  }
   
}



As you can see, there was a means to get a handle to OraclePreparedStatement and the connection type was of OracleConnection in c3p0 v. 8.4.5. But in version 8.5.1, from the NewProxyPreparedStatement object, I could not find a means to get the physical connection (OracleConnection) object nor the OraclePreparedStatement object. Does anybody know the solution to this problem.

I'm using Oracle 9i, hibernate 2.1.6 with c3p0 version 8.5.1.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 5:35 am 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
Hi,

1) Please use c3p0-0.8.5.2 (rather than 0.8.5.1) if you want to use the latest stable release. (c3p0-0.9.0-pre6 is quite near the final 0.9.0 release -- you might consider using this version.)

2) As a convenience, c3p0 ships with an extra jar, which contains an OracleUtils class for working with LOBs. See c3p0's docs (Appendix C). If you don't want to add the extra jar, you can do whatever you want or need to do with c3p0's rawConnectionOperation API. (Again, see c3p0's docs, which includes an Oracle CLOB example.)

Good luck!
Steve (c3p0 guy)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 2:16 pm 
Newbie

Joined: Mon Dec 13, 2004 6:54 pm
Posts: 2
Steve,

Thanks a bunch! That helped a lot. Now, things are up and running.


swaldman wrote:
Hi,

1) Please use c3p0-0.8.5.2 (rather than 0.8.5.1) if you want to use the latest stable release. (c3p0-0.9.0-pre6 is quite near the final 0.9.0 release -- you might consider using this version.)

2) As a convenience, c3p0 ships with an extra jar, which contains an OracleUtils class for working with LOBs. See c3p0's docs (Appendix C). If you don't want to add the extra jar, you can do whatever you want or need to do with c3p0's rawConnectionOperation API. (Again, see c3p0's docs, which includes an Oracle CLOB example.)

Good luck!
Steve (c3p0 guy)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.