Archive for the 'SQL Server' Category

Remove Oracle CLOB ClassCastException

Written by coregps on Friday, January 5th, 2007 in Java, SQL Server, Oracle.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I recently migrated from SQL Server 2000 to Oracle 8.1.7 and now I have to handle Oracle CLOB data. To demonstrate my problem, I will create the following table:

CREATE TABLE test(testid number, content clob);

The following code snippet is used to write CLOB data into database:

String sql = "INSERT INTO test(testid, content) VALUES(?, EMPTY_CLOB())";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, maxid);
pst.executeUpdate();
sql = "SELECT content FROM test WHERE testid=? FOR UPDATE";
pst = conn.prepareStatement(sql);
pst.setInt(1, maxid);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
    clob.putString(1, "Some content to write into database");
}

And the code used to convert CLOB data to String:

public static String clobToString(Object obj) {
    StringBuffer content= new StringBuffer("");
    if (obj != null) {
        CLOB clob = (CLOB)obj;
        Reader is;
        try {
            is = clob.getCharacterStream();
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            while (s != null) {
                content.append(s);
                s = br.readLine();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
    return content.toString();
}

I was able to successfully run the code above when I use the connection obtained from normal DriverManager.getConnection method.

DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
String url = “jdbc:oracle:thin:@ubuntu:1521:oradb”;
String userName = “scott”;
String password = “tiger”;
conn = DriverManager.getConnection(url, userName, password);

But got some trouble when I use a datasource lookup.

<Resource name="jdbc/oraDB" auth="Container" type="javax.sql.DataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
   url="jdbc:oracle:thin:@server:1521:oradb"
   username="scott"
   password="tiger"
   maxWait="-1" removeAbandoned="true"
   maxActive="20" maxIdle="10"
   removeAbandonedTimeout="180" logAbandoned="true" accessToUnderlyingConnectionAllowed="true" />

public class DBConn {
    private static Context initCtx = null;
	  private static DataSource ds = null;
    private static String resource = "java:comp/env/jdbc/oraDB";

  	/**
  	 * Get a connection from pool
  	 *
  	 * @return a Connection
  	 */
  	public static Connection getConnection() {
  		  Connection conn = null;
  		  try {
  			    if (initCtx == null) {
  				      initCtx = new InitialContext();
  			    }
  			    if (ds == null) {
  				      ds = (DataSource) initCtx.lookup(resource);
  			    }
  			    try {
  				      conn = ds.getConnection();
  			    } catch (SQLException e1) {
  			    }
  		  } catch (NamingException e) {
  		  }
  		  return conn;
  	}
}

The following line will throw a ClassCastException “java.lang.ClassCastException: oracle.sql.CLOB”.

oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);

I first found the following section in Oracle forum(http://forums.oracle.com/forums/message.jspa?messageID=855366):

It ONLY works if the Connection is an oracle.jdbc.driver.OracleConnection object. If it's not an oracle.jdbc.driver.OracleConnection, you get a ClassCastException.

The problem happens because when I run my code in Tomcat and get the database connection via a DataSource object that I lookup via JNDI, the connection is not a real OracleConnection object, but an object that wraps the real OracleConnection.

In more detail:

Tomcat uses the Apache Commons DBCP package (see http://jakarta.apache.org/commons/dbcp/) for database connection pooling. When you lookup a DataSource object using JNDI, like this:

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/ari");

the object that you get is an instance of class org.apache.commons.dbcp.BasicDataSource. When you call getConnection on this object, you get an instance of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper (which obviously implements java.sql.Connection, and wraps the OracleConnection object).

To be able to call CLOB.createTemporary successfully, we need to find the wrapped OracleConnection object. A number of steps are necessary to reach this:

First of all, by default the PoolingDataSource$PoolGuardConnectionWrapper does not allow access to the wrapped Connection object (don't ask me why, but that's how the Apache developers decided it should be). You must enable access by configuring it in the <context> of the web application (in Tomcat's server.xml configuration file), for example:

<context path="/ariweb" docBase="ariweb.war">
<resource name="jdbc/ari" auth="Container" type="javax.sql.DataSource"/>
<resourceparams name="jdbc/ari">
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<!-- NOTE: This is necessary to enable access to the Oracle connection object -->
<name>accessToUnderlyingConnectionAllowed</name>
<value>true</value>
</parameter>
<!-- Other configuration parameters -->
...
</resourceparams>
</context>

The PoolingDataSource$PoolGuardConnectionWrapper extends class org.apache.commons.dbcp.DelegatingConnection, which has a getDelegate method. We can call this method to get the wrapped connection.

Unfortunately, the story is not yet finished. The getDelegate method of the PoolingDataSource$PoolGuardConnectionWrapper does not return the OracleConnection object - it returns an instance of class org.apache.commons.dbcp.PoolableConnection, which is a second wrapper around the OracleConnection object.

The PoolableConnection class also extends DelegatingConnection, so we can call getDelegate again and finally we have the OracleConnection object.

So here is the final code:

// conn is the Connection I got from the DataSource
Connection oracleConnection = conn;

if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) {
// This returns a org.apache.commons.dbcp.PoolableConnection
Connection pc = ((org.apache.commons.dbcp.DelegatingConnection)conn).getDelegate();

// The PoolableConnection is a DelegatingConnection itself - get the delegate (the Oracle connection)
oracleConnection = ((org.apache.commons.dbcp.DelegatingConnection)pc).getDelegate();
}

CLOB clob = CLOB.createTemporary(oracleConnection, true, CLOB.DURATION_SESSION);
clob.open(CLOB.MODE_READWRITE);
...

However, after trying the solution above as following:

conn = DBConn.getConnection();
if (conn instanceof DelegatingConnection) {
    // This returns a org.apache.commons.dbcp.PoolableConnection
    Connection pc = ((DelegatingConnection)conn).getDelegate();

    // The PoolableConnection is a DelegatingConnection itself - get the delegate (the Oracle connection)
    conn = ((DelegatingConnection)pc).getDelegate();
}

I still got the same problem. Finally I found the right answer:

The two classes are the same (oracle.sql.CLOB), but they are loaded by different classloader. This usually happens when you put your JDBC driver jars both in server’s and webapp’s libraries and use the jndi datasource; the db objects (like CLOB) are created by the server (using its classloader) so when the webapp tries to cast those objects it gets that error, even if everything works fine when not using casts; you probably would have the same issue if you try to cast the ResultSet to OracleResultSet. I would recommend to put the JDBC driver(s) jars ONLY in the server lib dir; In Tomcat it would be the commons directory.

So, It is not necessary to get the wrapped OracleConnection object. Just put the Oracle jdbc driver ONLY in the Tomcat\Common\lib directory.

Hope this helps if you meet the same trouble.

How to pass database as parameter in stored procedure

Written by coregps on Friday, December 22nd, 2006 in SQL Server.

I want to write a stored procedure which is used to merge many databases into a single one. This stored procedure contains one parameter named @db_name. It is something like this first:

CREATE PROCEDURE batchimport
@db_name varchar(20)
AS
INSERT INTO employee SELECT * FROM @db_name..employee
GO

But I always get the syntax error. It seems that the parameter can’t be used to pass field names, table names and database names. So I implement the new one using dynamically-generated SQL strings.

/*
Function: Merge many databases into a single one
Call: exec batchimport ‘database name’
*/

CREATE PROCEDURE batchimport
@db_name varchar(20)
AS

BEGIN
DECLARE @sql nvarchar(4000)

SELECT @sql = ‘INSERT INTO employee SELECT * FROM ‘ + quotename(@db_name) + ‘..employee’
EXEC sp_executesql @sql

END

GO

It should be noticed that the sp_executesql need a nvarchar data type on its parameter.

How to delete duplicate rows From an Oracle Table

Written by coregps on Monday, December 18th, 2006 in SQL Server, Oracle.

I have a Oracle table which has some duplicate rows after importing data from SQL Server twice. The following SQL statement can be used to delete the duplicate entries.

DELETE FROM table_name
WHERE rowid not in
(SELECT MIN(rowid)
FROM table_name
GROUP BY column1, column2, column3…));

The subquery is used to find out a unique rowid from all the rowid’s. It can be MAX or MIN, and the group by clause should include all the UNIQUE columns. The fields column1, column2, column3… constitute the identifying key for each record.

The the subquery returns one record for the dupliacte ID’s and I delete all of them that are not in the subquery and that deletes all the dupliacte rows from the database.

Get SQLNestedException again

Written by coregps on Saturday, November 5th, 2005 in Java, SQL Server.

When I configured SQL Server connection pool in Tomcat 5.5.9, I got the SQLNestedException again.


org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘’ for connect URL ‘null’
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
 at org.apache.commons.dbutils.QueryRunner.prepareConnection(QueryRunner.java:189)
 at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:300)
 at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:322)
 at com.homesoft.db.DBConn.searchToMapList(DBConn.java:278)
 at platform.Employee.getEmployeeList(Employee.java:27)
 at org.apache.jsp.newtask_jsp._jspService(org.apache.jsp.newtask_jsp:110)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
 at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
 at java.sql.DriverManager.getDriver(Unknown Source)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
 … 27 more

I reviewed my configuration again and again, but can’t find out the reason. The following is the JNDI DataSource configuration in Tomcat server.xml file:


<Context path=”" reloadable=”true” docBase=”d:/eclipse/workspace/demo” debug=”0″>
    <Resource name=”jdbc/demoDB” auth=”Container” type=”javax.sql.DataSource”
    factory=”org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory”
    username=”sa”
    password=”secret”
    driverClassName=”com.microsoft.jdbc.sqlserver.SQLServerDriver”
    url=”jdbc:microsoft:sqlserver://localhost:1433;selectMethod=cursor;DatabaseName=demodb”
    maxWait=”1000″ removeAbandoned=”true”
    maxActive=”100″ maxIdle=”30″
    removeAbandonedTimeout=”60″ logAbandoned=”true”/>
</Context>

Finally, I found that I’ve not created the web application deployment descriptor file - web.xml. What a stupid mistake!!!


You may similarily have received or will receive the SQLNestedException. In my experience, the following key points should be noticed carefully:


1. The JNDI DataSource should be both defined inside the server.xml and declared in the web.xml.


2. We have to set up the JNDI resources by attribute instead of resource-params like we used to.


3. The value of the ‘factory’ attribute of the ‘Resource’ element should be “org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory” instead of “org.apache.commons.dbcp.BasicDataSourceFactory”.

Select random records from SQL Server 2000

Written by coregps on Friday, July 29th, 2005 in SQL Server.

I’m working on an online exam project in jsp using SQL Server 2000 database. In which I need some way to quickly pull out a certain number of random records (questions) for each subject and the type of the question from database and generate the exam paper. I used the following solution, it is very simple.

SELECT TOP N * FROM tablename ORDER BY NewID()

Where N is the number of records that you want, the NewID() function returns a new Unique Identifier(GUID). When the query runs, a column with GUIDs is created and the results are sorted before the N records are retrieved.

The following T-SQL is my code snippet used to select random questions from database.

SELECT TOP 10 * FROM questions WHERE (subjectId = ’s1′ AND typeId = ‘t1′) ORDER BY NewID()



Site Navigation