HHH-5961 : Contextual LOB creator is used when the JDBC driver does not support JDBC4 Connection.createBlob()

This commit is contained in:
Gail Badner 2011-03-08 21:14:29 -08:00
parent ad5f88c2d6
commit 32577a8a79
2 changed files with 493 additions and 0 deletions

View File

@ -0,0 +1,136 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
* third-party contributors as indicated by either @author tags or express
* copyright attribution statements applied by the authors. All
* third-party contributions are distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.jdbc.internal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DatabaseMetaData;
import java.lang.reflect.Method;
import java.util.Map;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.ContextualLobCreator;
import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Builds {@link LobCreator} instances based on the capabilities of the environment.
*
* @author Steve Ebersole
*/
public class LobCreatorBuilder {
private static final Logger log = LoggerFactory.getLogger( LobCreatorBuilder.class );
private boolean useContextualLobCreation;
/**
* The public factory method for obtaining the appropriate (according to given JDBC {@link java.sql.Connection}.
*
*
* @param jdbcConnection A JDBC {@link java.sql.Connection} which can be used to gauge the drivers level of support,
* specifically for creating LOB references.
*/
public LobCreatorBuilder(Map configValues, Connection jdbcConnection) {
this.useContextualLobCreation = useContextualLobCreation( configValues, jdbcConnection );
}
private static final Class[] NO_ARG_SIG = new Class[0];
private static final Object[] NO_ARGS = new Object[0];
/**
* Basically here we are simply checking whether we can call the {@link Connection} methods for
* LOB creation added in JDBC 4. We not only check whether the {@link Connection} declares these methods,
* but also whether the actual {@link Connection} instance implements them (i.e. can be called without simply
* throwing an exception).
*
* @param jdbcConnection The connection which can be used in level-of-support testing.
*
* @return True if the connection can be used to create LOBs; false otherwise.
*/
private static boolean useContextualLobCreation(Map configValues, Connection jdbcConnection) {
boolean isNonContextualLobCreationRequired =
ConfigurationHelper.getBoolean( Environment.NON_CONTEXTUAL_LOB_CREATION, configValues );
if ( isNonContextualLobCreationRequired ) {
log.info( "Disabling contextual LOB creation as " + Environment.NON_CONTEXTUAL_LOB_CREATION + " is true" );
return false;
}
if ( jdbcConnection == null ) {
log.info( "Disabling contextual LOB creation as connection was null" );
return false;
}
try {
try {
DatabaseMetaData meta = jdbcConnection.getMetaData();
// if the jdbc driver version is less than 4, it shouldn't have createClob
if ( meta.getJDBCMajorVersion() < 4 ) {
log.info(
"Disabling contextual LOB creation as JDBC driver reported JDBC version [" +
meta.getJDBCMajorVersion() + "] less than 4"
);
return false;
}
}
catch ( SQLException ignore ) {
// ignore exception and continue
}
Class connectionClass = Connection.class;
Method createClobMethod = connectionClass.getMethod( "createClob", NO_ARG_SIG );
if ( createClobMethod.getDeclaringClass().equals( Connection.class ) ) {
// If we get here we are running in a jdk 1.6 (jdbc 4) environment...
// Further check to make sure the driver actually implements the LOB creation methods. We
// check against createClob() as indicative of all; should we check against all 3 explicitly?
try {
Object clob = createClobMethod.invoke( jdbcConnection, NO_ARGS );
try {
Method freeMethod = clob.getClass().getMethod( "free", NO_ARG_SIG );
freeMethod.invoke( clob, NO_ARGS );
}
catch ( Throwable ignore ) {
log.trace( "Unable to free CLOB created to test createClob() implementation : " + ignore );
}
return true;
}
catch ( Throwable t ) {
log.info( "Disabling contextual LOB creation as createClob() method threw error : " + t );
}
}
}
catch ( NoSuchMethodException ignore ) {
}
return false;
}
public LobCreator buildLobCreator(LobCreationContext lobCreationContext) {
return useContextualLobCreation
? new ContextualLobCreator( lobCreationContext )
: NonContextualLobCreator.INSTANCE;
}
}

View File

@ -0,0 +1,357 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jdbc;
import java.sql.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import junit.framework.TestCase;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.internal.LobCreatorBuilder;
import org.hibernate.engine.jdbc.ContextualLobCreator;
import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.BlobImplementer;
import org.hibernate.engine.jdbc.ClobImplementer;
import org.hibernate.engine.jdbc.NClobImplementer;
import org.hibernate.engine.jdbc.WrappedBlob;
import org.hibernate.engine.jdbc.WrappedClob;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class LobCreatorTest extends TestCase {
public void testConnectedLobCreator() throws SQLException {
final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( true ) );
LobCreationContext lobCreationContext = new LobCreationContextImpl( connection );
LobCreator lobCreator =
new LobCreatorBuilder( new Properties(), connection )
.buildLobCreator( lobCreationContext );
assertTrue( lobCreator instanceof ContextualLobCreator );
testLobCreation( lobCreator );
connection.close();
}
public void testJdbc3LobCreator() throws SQLException {
final Connection connection = createConnectionProxy( 3, new JdbcLobBuilderImpl( false) );
LobCreationContext lobCreationContext = new LobCreationContextImpl( connection );
LobCreator lobCreator =
new LobCreatorBuilder( new Properties(), connection )
.buildLobCreator( lobCreationContext );
assertSame( NonContextualLobCreator.INSTANCE, lobCreator );
testLobCreation( lobCreator );
connection.close();
}
public void testJdbc4UnsupportedLobCreator() throws SQLException {
final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( false ) );
LobCreationContext lobCreationContext = new LobCreationContextImpl( connection );
LobCreator lobCreator =
new LobCreatorBuilder( new Properties(), connection )
.buildLobCreator( lobCreationContext );
assertSame( NonContextualLobCreator.INSTANCE, lobCreator );
testLobCreation( lobCreator );
connection.close();
}
public void testConfiguredNonContextualLobCreator() throws SQLException {
final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( true ) );
LobCreationContext lobCreationContext = new LobCreationContextImpl( connection );
Properties props = new Properties();
props.setProperty( Environment.NON_CONTEXTUAL_LOB_CREATION, "true" );
LobCreator lobCreator =
new LobCreatorBuilder( props, connection )
.buildLobCreator( lobCreationContext );
assertSame( NonContextualLobCreator.INSTANCE, lobCreator );
testLobCreation( lobCreator );
connection.close();
}
private void testLobCreation(LobCreator lobCreator) throws SQLException{
Blob blob = lobCreator.createBlob( new byte[] {} );
if ( lobCreator == NonContextualLobCreator.INSTANCE ) {
assertTrue( blob instanceof BlobImplementer );
}
else {
assertTrue( blob instanceof JdbcBlob );
}
blob = lobCreator.wrap( blob );
assertTrue( blob instanceof WrappedBlob );
Clob clob = lobCreator.createClob( "Hi" );
if ( lobCreator == NonContextualLobCreator.INSTANCE ) {
assertTrue( clob instanceof ClobImplementer );
}
else {
assertTrue( clob instanceof JdbcClob );
}
clob = lobCreator.wrap( clob );
assertTrue( clob instanceof WrappedClob );
Clob nclob = lobCreator.createNClob( "Hi" );
if ( lobCreator == NonContextualLobCreator.INSTANCE ) {
assertTrue( nclob instanceof NClobImplementer );
}
else {
assertTrue( nclob instanceof JdbcNClob );
}
assertTrue( NClob.class.isInstance( nclob ) );
nclob = lobCreator.wrap( nclob );
assertTrue( nclob instanceof WrappedClob );
blob.free();
clob.free();
nclob.free();
}
private class LobCreationContextImpl implements LobCreationContext {
private Connection connection;
private LobCreationContextImpl(Connection connection) {
this.connection = connection;
}
public Object execute( LobCreationContext.Callback callback) {
try {
return callback.executeOnConnection( connection );
}
catch ( SQLException e ) {
throw new RuntimeException( "Unexpected SQLException", e );
}
}
}
private interface JdbcLobBuilder {
public Blob createBlob() throws SQLException ;
public Clob createClob() throws SQLException ;
public NClob createNClob() throws SQLException ;
}
private class JdbcLobBuilderImpl implements JdbcLobBuilder {
private final boolean isSupported;
private JdbcLobBuilderImpl(boolean isSupported) {
this.isSupported = isSupported;
}
public Blob createBlob() throws SQLException {
if ( ! isSupported ) {
throw new SQLException( "not supported!" );
}
return new JdbcBlob();
}
public Clob createClob() throws SQLException {
if ( ! isSupported ) {
throw new SQLException( "not supported!" );
}
return new JdbcClob();
}
public NClob createNClob() throws SQLException {
if ( ! isSupported ) {
throw new SQLException( "not supported!" );
}
return new JdbcNClob();
}
}
private class ConnectionProxyHandler implements InvocationHandler {
private final JdbcLobBuilder lobBuilder;
private final DatabaseMetaData metadata;
private ConnectionProxyHandler(int version, JdbcLobBuilder lobBuilder) {
this.lobBuilder = lobBuilder;
this.metadata = createMetadataProxy( version );
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// the only methods we are interested in are the LOB creation methods...
if ( args == null || args.length == 0 ) {
final String methodName = method.getName();
if ( "createBlob".equals( methodName ) ) {
return lobBuilder.createBlob();
}
else if ( "createClob".equals( methodName ) ) {
return lobBuilder.createClob();
}
else if ( "createNClob".equals( methodName ) ) {
return lobBuilder.createNClob();
}
else if ( "getMetaData".equals( methodName ) ) {
return metadata;
}
}
return null;
}
}
private static Class[] CONN_PROXY_TYPES = new Class[] { Connection.class };
private Connection createConnectionProxy(int version, JdbcLobBuilder jdbcLobBuilder) {
ConnectionProxyHandler handler = new ConnectionProxyHandler( version, jdbcLobBuilder );
return ( Connection ) Proxy.newProxyInstance( getClass().getClassLoader(), CONN_PROXY_TYPES, handler );
}
private class MetadataProxyHandler implements InvocationHandler {
private final int jdbcVersion;
private MetadataProxyHandler(int jdbcVersion) {
this.jdbcVersion = jdbcVersion;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ( "getJDBCMajorVersion".equals( methodName ) ) {
return jdbcVersion;
}
return null;
}
}
private static Class[] META_PROXY_TYPES = new Class[] { DatabaseMetaData.class };
private DatabaseMetaData createMetadataProxy(int version) {
MetadataProxyHandler handler = new MetadataProxyHandler( version );
return ( DatabaseMetaData ) Proxy.newProxyInstance( getClass().getClassLoader(), META_PROXY_TYPES, handler );
}
private class JdbcBlob implements Blob {
public long length() throws SQLException {
return 0;
}
public byte[] getBytes(long pos, int length) throws SQLException {
return new byte[0];
}
public InputStream getBinaryStream() throws SQLException {
return null;
}
public long position(byte[] pattern, long start) throws SQLException {
return 0;
}
public long position(Blob pattern, long start) throws SQLException {
return 0;
}
public int setBytes(long pos, byte[] bytes) throws SQLException {
return 0;
}
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
return 0;
}
public OutputStream setBinaryStream(long pos) throws SQLException {
return null;
}
public void truncate(long len) throws SQLException {
}
public void free() throws SQLException {
}
public InputStream getBinaryStream(long pos, long length) throws SQLException {
return null;
}
}
private class JdbcClob implements Clob {
public long length() throws SQLException {
return 0;
}
public String getSubString(long pos, int length) throws SQLException {
return null;
}
public Reader getCharacterStream() throws SQLException {
return null;
}
public InputStream getAsciiStream() throws SQLException {
return null;
}
public long position(String searchstr, long start) throws SQLException {
return 0;
}
public long position(Clob searchstr, long start) throws SQLException {
return 0;
}
public int setString(long pos, String str) throws SQLException {
return 0;
}
public int setString(long pos, String str, int offset, int len) throws SQLException {
return 0;
}
public OutputStream setAsciiStream(long pos) throws SQLException {
return null;
}
public Writer setCharacterStream(long pos) throws SQLException {
return null;
}
public void truncate(long len) throws SQLException {
}
public void free() throws SQLException {
}
public Reader getCharacterStream(long pos, long length) throws SQLException {
return null;
}
}
private class JdbcNClob extends JdbcClob implements NClob {
}
}