© 2001 BEA Systems, Inc.

com.beasys.commerce.util
Class JdbcHelper

java.lang.Object
  |
  +--com.beasys.commerce.util.JdbcHelper

public class JdbcHelper
extends java.lang.Object


Field Summary
static boolean shouldUseClobsForReads
          Controls whether we should use the Clob object or the ResultSet.getString() method to retrieve CLOB columns.
static boolean shouldUseClobsForWrites
          Controls whether we should use the setCharacterStream() or the ResultSet.setString() method to set CLOB columns.
static java.lang.Boolean supportsLikeEscapeClause
          Determine if the server configuration supports the LIKE ESCAPE clause.
 
Constructor Summary
JdbcHelper()
          Helper constructor comment.
 
Method Summary
static void close(java.sql.Connection con)
          Close a connection, ignoring any errors.
static void close(java.sql.ResultSet rs)
          Close a result set, ignoring any errors.
static void close(java.sql.Statement stmt)
          Close a statement, ignoring any errors.
static java.lang.Object deserialize(java.sql.ResultSet rs, int index)
          Convenience method for de-serializing an object from a JDBC source.
static java.lang.String getClob(java.sql.ResultSet rs, int index)
          Get the value of a CLOB column as a string.
static java.io.Reader getClobReader(java.sql.ResultSet rs, int index)
          Get the value of a CLOB column as a reader.
static java.sql.Connection getConnection(javax.sql.DataSource src, int maxRetries)
          Get a connection from a DataSource with retry logic.
static java.sql.Connection getConnection(javax.sql.DataSource src, int maxRetries, long waitTime)
          Get a connection from a DataSource with retry and timeout logic.
static double getDouble(java.sql.ResultSet rs, int index)
          Retrieve a double value from the ResultSet at the given index.
static double getDouble(java.sql.ResultSet rs, java.lang.String colName)
          Retrieve a double value from the ResultSet at the given column.
static float getFloat(java.sql.ResultSet rs, int index)
          Retrieve a float value from the ResultSet at the given index.
static float getFloat(java.sql.ResultSet rs, java.lang.String colName)
          Retrieve a float value from the ResultSet at the given column.
static int getInt(java.sql.ResultSet rs, int index)
          Retrieve an int value from the ResultSet at the given index.
static int getInt(java.sql.ResultSet rs, java.lang.String colName)
          Retrieve an int value from the ResultSet at the given column.
static long getLong(java.sql.ResultSet rs, int index)
          Retrieve a long value from the ResultSet at the given index.
static long getLong(java.sql.ResultSet rs, java.lang.String colName)
          Retrieve a long value from the ResultSet at the given column.
static boolean isNoResourceException(java.sql.SQLException ex)
          Tell if a SQLException is telling that no resources/connections are available right now.
static void serialize(java.sql.PreparedStatement ps, int index, java.io.Serializable obj)
          Convenience method for serializing an object to a JDBC stream.
static void setClob(java.sql.PreparedStatement stmt, int index, java.io.Reader in, int length)
          Set the value of a CLOB column with an input reader.
static void setClob(java.sql.PreparedStatement stmt, int index, java.lang.String str)
          Set the value of a CLOB column with a string.
static boolean supportsLikeEscapeClause(java.sql.Connection con)
          Determine if the given connection supports the LIKE ESCAPE clause.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

supportsLikeEscapeClause

public static final java.lang.Boolean supportsLikeEscapeClause
Determine if the server configuration supports the LIKE ESCAPE clause.

This variable will initialized from the "commerce.jdbc.supportsLikeEscapeClause" property in weblogiccommerce.properties.

If this is null, then no configuration was specified and the JDBC connection should be queries. Otherwise, this will be true or false.


shouldUseClobsForReads

public static final boolean shouldUseClobsForReads
Controls whether we should use the Clob object or the ResultSet.getString() method to retrieve CLOB columns.

This variable will initialized from the "commerce.jdbc.read.shouldUseClobs" property in weblogiccommerce.properties. Defaults to false


shouldUseClobsForWrites

public static final boolean shouldUseClobsForWrites
Controls whether we should use the setCharacterStream() or the ResultSet.setString() method to set CLOB columns.

This variable will initialized from the "commerce.jdbc.write.shouldUseClobs" property in weblogiccommerce.properties. Defaults to false

Constructor Detail

JdbcHelper

public JdbcHelper()
Helper constructor comment.
Method Detail

getConnection

public static java.sql.Connection getConnection(javax.sql.DataSource src,
                                                int maxRetries,
                                                long waitTime)
                                         throws java.sql.SQLException
Get a connection from a DataSource with retry and timeout logic.

This will retry upto maxRetries. Additionally, it will only retry for waitTime number of milliseconds (as best as possible -- it might be longer, but will not be less).

This will always try at least once to get a connection. Use maxRetries=0 for only a single attempt.

This works with both transactional and non-transaction DataSources tied to WLS connection pools. In WLS, transactional DataSources always internally wait 5 seconds for a new connection; non-transaction DataSources don't wait at all.

Parameters:
src - the data source.
maxRetries - the maximum number of times to retry (less than 0 for infinite).
waitTime - the maximum number of milliseconds to try (less than 0 for infinite).
Returns:
a connection from the data source on success.
Throws:
java.sql.SQLException - thrown on an error getting a connection.
See Also:
isNoResourceException(java.sql.SQLException)

getConnection

public static java.sql.Connection getConnection(javax.sql.DataSource src,
                                                int maxRetries)
                                         throws java.sql.SQLException
Get a connection from a DataSource with retry logic.
Parameters:
src - the data source.
maxRetries - the maximum number of times to retry (less than 0 for infinite).
Returns:
a connection from the data source on success.
Throws:
java.sql.SQLException - thrown on an error getting a connection.

isNoResourceException

public static boolean isNoResourceException(java.sql.SQLException ex)
Tell if a SQLException is telling that no resources/connections are available right now.

When WLS doesn't have an available connection for a DataSource (either TX or non-TX), it will throw a SQLException with a message following this regular expression: "ResourceException.+[Nn]o.+available". That seems to the only way to tell if a connection wasn't availble from the underlying pool.


serialize

public static void serialize(java.sql.PreparedStatement ps,
                             int index,
                             java.io.Serializable obj)
                      throws java.sql.SQLException,
                             java.io.IOException
Convenience method for serializing an object to a JDBC stream. Serializes the object and calls setBinaryStream() on the prepared statement. The data will be written out later when the statment is executed. Note: getBinaryStream() requires the size of the data bytes, so cannot use PipeInputStream, PipeOutputStream
Parameters:
ps - PreparedStatement.
index - placeholder index of the serialized object.
obj - Object to be serialized.

deserialize

public static java.lang.Object deserialize(java.sql.ResultSet rs,
                                           int index)
                                    throws java.sql.SQLException,
                                           java.io.IOException,
                                           java.io.StreamCorruptedException,
                                           java.lang.ClassNotFoundException
Convenience method for de-serializing an object from a JDBC source. Note: This method may be able to use PipedOutputStream and PipedInputStream.
Parameters:
ps - Result Set.
index - table column index.

supportsLikeEscapeClause

public static boolean supportsLikeEscapeClause(java.sql.Connection con)
Determine if the given connection supports the LIKE ESCAPE clause.

This first checks the supportsLikeEscapeClause varaible. It then checks with the Connection DatabaseMetaData.


getInt

public static int getInt(java.sql.ResultSet rs,
                         int index)
                  throws java.sql.SQLException
Retrieve an int value from the ResultSet at the given index.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
index - the column index.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getInt

public static int getInt(java.sql.ResultSet rs,
                         java.lang.String colName)
                  throws java.sql.SQLException
Retrieve an int value from the ResultSet at the given column.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
colName - the column name.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getLong

public static long getLong(java.sql.ResultSet rs,
                           int index)
                    throws java.sql.SQLException
Retrieve a long value from the ResultSet at the given index.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
index - the column index.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getLong

public static long getLong(java.sql.ResultSet rs,
                           java.lang.String colName)
                    throws java.sql.SQLException
Retrieve a long value from the ResultSet at the given column.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
colName - the column name.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getFloat

public static float getFloat(java.sql.ResultSet rs,
                             int index)
                      throws java.sql.SQLException
Retrieve a float value from the ResultSet at the given index.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
index - the column index.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getFloat

public static float getFloat(java.sql.ResultSet rs,
                             java.lang.String colName)
                      throws java.sql.SQLException
Retrieve a float value from the ResultSet at the given column.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
colName - the column name.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getDouble

public static double getDouble(java.sql.ResultSet rs,
                               int index)
                        throws java.sql.SQLException
Retrieve a double value from the ResultSet at the given index.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
index - the column index.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getDouble

public static double getDouble(java.sql.ResultSet rs,
                               java.lang.String colName)
                        throws java.sql.SQLException
Retrieve a double value from the ResultSet at the given column.

This will validate if the underlying column was null.

Parameters:
rs - the result set.
colName - the column name.
Returns:
the column value (0 for null column).
Throws:
java.sql.SQLException - from retrieving the column.

getClob

public static java.lang.String getClob(java.sql.ResultSet rs,
                                       int index)
                                throws java.sql.SQLException
Get the value of a CLOB column as a string.

This is controlled by the value of shouldUseClobsForReads.

Parameters:
rs - the result set.
index - the column index in the result set.
Returns:
the column as a string.
Throws:
java.sql.SQLException - thrown on an error.

getClobReader

public static java.io.Reader getClobReader(java.sql.ResultSet rs,
                                           int index)
                                    throws java.sql.SQLException
Get the value of a CLOB column as a reader.

This is controlled by the value of shouldUseClobsForReads.

Parameters:
rs - the result set.
index - the column index in the result set.
Returns:
the column as a reader, null on null.
Throws:
java.sql.SQLException - thrown on an error.

setClob

public static void setClob(java.sql.PreparedStatement stmt,
                           int index,
                           java.lang.String str)
                    throws java.sql.SQLException
Set the value of a CLOB column with a string.

This is controlled by the value of shouldUseClobsForWrites.

Parameters:
stmt - the prepared statement.
index - the column index.
str - the CLOB string.
Throws:
java.sql.SQLException - thrown on an error.

setClob

public static void setClob(java.sql.PreparedStatement stmt,
                           int index,
                           java.io.Reader in,
                           int length)
                    throws java.sql.SQLException
Set the value of a CLOB column with an input reader.

This is controlled by the value of shouldUseClobsForWrites.

Parameters:
stmt - the prepared statement.
index - the column index.
in - the CLOB input reader.
Throws:
java.sql.SQLException - thrown on an error.

close

public static void close(java.sql.Connection con)
Close a connection, ignoring any errors.

close

public static void close(java.sql.Statement stmt)
Close a statement, ignoring any errors.

close

public static void close(java.sql.ResultSet rs)
Close a result set, ignoring any errors.

© 2001 BEA Systems, Inc.

Copyright © 2001 BEA Systems, Inc. All Rights Reserved