[hibernate-commits] Hibernate SVN: r13926 - in sandbox/trunk/jdbc-proxy/src: main/java/org/hibernate/jdbc/impl and 3 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Thu Aug 16 04:13:46 EDT 2007
Author: steve.ebersole at jboss.com
Date: 2007-08-16 04:13:46 -0400 (Thu, 16 Aug 2007)
New Revision: 13926
Removed:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java
Modified:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/ConnectionObserver.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImpl.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImplementor.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
Log:
javadocs + initial after_statement release logic
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -19,19 +19,57 @@
import java.sql.Statement;
/**
- * JDBCContainer contract
+ * Contract for a JDBCContainer, which acts as a container (or holder) for JDBC objects.
*
* @author Steve Ebersole
*/
public interface JDBCContainer {
+ /**
+ * Register a JDBC statement with the container.
+ *
+ * @param statement The statement to register.
+ */
public void register(Statement statement);
+
+ /**
+ * Release a statement (un-register it).
+ *
+ * @param statement The statement to release.
+ */
public void release(Statement statement);
+ /**
+ * Register a JDBC resultset with the container.
+ *
+ * @param resultSet The resultset to register.
+ */
public void register(ResultSet resultSet);
+
+ /**
+ * Release a resultset (un-register it).
+ *
+ * @param resultSet The resultset to release.
+ */
public void release(ResultSet resultSet);
+ /**
+ * Does this container currently have registered resources?
+ *
+ * @return True if the container does have registered resources; false otherwise.
+ */
public boolean hasRegisteredResources();
+ /**
+ * Release all registered resources.
+ */
public void releaseResources();
+
+ /**
+ * Close this container. Also releases resources, but additionally indicates that
+ * the container is expected to no longer be used. As such the implemntation is
+ * free to render itself unusable.
+ *
+ * @see #releaseResources()
+ */
public void close();
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -24,5 +24,10 @@
* @author Steve Ebersole
*/
public interface JDBCContainerBuilder {
+ /**
+ * Build a container.
+ *
+ * @return The container.
+ */
public JDBCContainer buildJdbcContainer();
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -20,13 +20,37 @@
import org.hibernate.jdbc.util.SQLStatementLogger;
/**
- * Contract for services around JDBC operations.
+ * Contract for services around JDBC operations. These represent
+ * shared resources, aka not varied by session/use.
*
* @author Steve Ebersole
*/
public interface JDBCServices {
+ /**
+ * Obtain service for providing JDBC connections.
+ *
+ * @return The connection provider.
+ */
public ConnectionProvider getConnectionProvider();
+
+ /**
+ * Obtain service for building JDBCContainers instances.
+ *
+ * @return The JDBCContainer builder.
+ */
public JDBCContainerBuilder getJdbcContainerBuilder();
+
+ /**
+ * Obtain service for logging SQL statements.
+ *
+ * @return The SQL statement logger.
+ */
public SQLStatementLogger getSqlStatementLogger();
+
+ /**
+ * Obtain service for dealing with exceptions.
+ *
+ * @return The exception helper service.
+ */
public ExceptionHelper getExceptionHelper();
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/ConnectionObserver.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/ConnectionObserver.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/ConnectionObserver.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -15,13 +15,28 @@
*/
package org.hibernate.jdbc.impl;
+import java.sql.Connection;
+
/**
- * ConnectionObserver contract
+ * An observer of logical connection events.
*
* @author Steve Ebersole
*/
public interface ConnectionObserver {
- public void physicalConnectionObtained();
+ /**
+ * A physical connection was obtained.
+ *
+ * @param connection The physical connection just obtained.
+ */
+ public void physicalConnectionObtained(Connection connection);
+
+ /**
+ * A physical connection was released.
+ */
public void physicalConnectionReleased();
+
+ /**
+ * The logical connection was closed.
+ */
public void logicalConnectionClosed();
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImpl.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImpl.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImpl.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -43,6 +43,7 @@
private final JDBCServices jdbcServices;
private final JDBCContainer jdbcContainer;
private final List observers = new ArrayList();
+ private boolean releasesEnabled = true;
private final boolean isUserSuppliedConnection;
private boolean isClosed;
@@ -51,6 +52,11 @@
Connection userSuppliedConnection,
ConnectionReleaseMode connectionReleaseMode,
JDBCServices jdbcServices) {
+ if ( connectionReleaseMode == ConnectionReleaseMode.AFTER_STATEMENT &&
+ ! jdbcServices.getConnectionProvider().supportsAggressiveRelease() ) {
+ log.debug( "connection provider reports to not support aggressive release; overriding" );
+ connectionReleaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
+ }
this.physicalConnection = userSuppliedConnection;
this.connectionReleaseMode = connectionReleaseMode;
this.jdbcServices = jdbcServices;
@@ -69,13 +75,6 @@
/**
* {@inheritDoc}
*/
- public ConnectionReleaseMode getConnectionReleaseMode() {
- return connectionReleaseMode;
- }
-
- /**
- * {@inheritDoc}
- */
public JDBCContainer getJdbcContainer() {
return jdbcContainer;
}
@@ -151,15 +150,56 @@
// }
/**
+ * {@inheritDoc}
+ */
+ public ConnectionReleaseMode getConnectionReleaseMode() {
+ return connectionReleaseMode;
+ }
+
+ public void afterStatementExecution() {
+ log.trace( "starting after statement execution processing [{}]", connectionReleaseMode );
+ if ( connectionReleaseMode == ConnectionReleaseMode.AFTER_STATEMENT ) {
+ if ( ! releasesEnabled ) {
+ log.debug( "skipping aggressive release due to manual disabling" );
+ return;
+ }
+ if ( jdbcContainer.hasRegisteredResources() ) {
+ log.debug( "skipping aggressive release due to registered resources" );
+ return;
+ }
+ releaseConnection();
+ }
+ }
+
+ public void afterTransaction() {
+ if ( connectionReleaseMode == ConnectionReleaseMode.AFTER_STATEMENT ||
+ connectionReleaseMode == ConnectionReleaseMode.AFTER_TRANSACTION ) {
+ if ( jdbcContainer.hasRegisteredResources() ) {
+ log.info( "forcing container resource cleanup on transaction completion" );
+ jdbcContainer.releaseResources();
+ }
+ aggressiveRelease();
+ }
+ }
+
+ public void disableReleases() {
+ log.trace( "disabling releases" );
+ releasesEnabled = false;
+ }
+
+ public void enableReleases() {
+ log.trace( "(re)enabling releases" );
+ releasesEnabled = true;
+ afterStatementExecution();
+ }
+
+ /**
* Force aggresive release of the underlying connection.
*/
public void aggressiveRelease() {
if ( isUserSuppliedConnection ) {
log.debug( "cannot aggresively release user-supplied connection; skipping" );
}
- else if ( ! getJdbcServices().getConnectionProvider().supportsAggressiveRelease() ) {
- log.debug( "connection provider reports to not support aggreswsive release; skipping" );
- }
else {
log.debug( "aggressively releasing JDBC connection" );
if ( physicalConnection != null ) {
@@ -179,7 +219,7 @@
try {
physicalConnection = getJdbcServices().getConnectionProvider().getConnection();
for ( Iterator itr = observers.iterator(); itr.hasNext(); ) {
- ( ( ConnectionObserver ) itr.next() ).physicalConnectionObtained();
+ ( ( ConnectionObserver ) itr.next() ).physicalConnectionObtained( physicalConnection );
}
log.debug( "obtained JDBC connection" );
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImplementor.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImplementor.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImplementor.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -26,14 +26,55 @@
* @author Steve Ebersole
*/
public interface LogicalConnectionImplementor extends LogicalConnection {
-
+ /**
+ * Obtains the JDBC services associated with this logical connection.
+ *
+ * @return JDBC services
+ */
public JDBCServices getJdbcServices();
- public ConnectionReleaseMode getConnectionReleaseMode();
-
+ /**
+ * Obtains the JDBC object container associated with this logical connection.
+ *
+ * @return The JDBC object container.
+ */
public JDBCContainer getJdbcContainer();
+ /**
+ * Add an observer interested in notification of connection events.
+ *
+ * @param observer The observer.
+ */
public void addObserver(ConnectionObserver observer);
- public void aggressiveRelease();
+ /**
+ * The release mode under which this logical connection is operating.
+ *
+ * @return the release mode.
+ */
+ public ConnectionReleaseMode getConnectionReleaseMode();
+
+ /**
+ * Used to signify that a statement has completed execution which may
+ * indicate that this logical connection need to perform an
+ * aggressive release of its physical connection.
+ */
+ public void afterStatementExecution();
+
+ /**
+ * Used to signify that a transaction has completed which may indicate
+ * that this logical connection need to perform an aggressive release
+ * of its physical connection.
+ */
+ public void afterTransaction();
+
+ /**
+ * Manually (and termporarily) circumvent aggressive release processing.
+ */
+ public void disableReleases();
+
+ /**
+ * Re enable aggressive release processing (after a prior {@link #disableReleases()} call.
+ */
+ public void enableReleases();
}
Deleted: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -1,139 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Steve Ebersole
- */
-package org.hibernate.jdbc.proxy;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.hibernate.HibernateException;
-import org.hibernate.jdbc.JDBCContainer;
-import org.hibernate.jdbc.JDBCServices;
-
-/**
- * AbstractStatementProxy implementation
- *
- * @author Steve Ebersole
- */
-public abstract class AbstractStatementProxy implements InvocationHandler {
-
- private static final Logger log = LoggerFactory.getLogger( AbstractStatementProxy.class );
-
- private boolean valid = true;
- private Statement statement;
- private ConnectionProxy connectionProxy;
-
- protected AbstractStatementProxy(Statement statement, ConnectionProxy connectionProxy) {
- this.statement = statement;
- this.connectionProxy = connectionProxy;
- }
-
- protected Statement getStatement() {
- if ( !valid ) {
- throw new HibernateException( "statment proxy is no longer valid" );
- }
- return statement;
- }
-
- protected ConnectionProxy getConnectionProxy() {
- return connectionProxy;
- }
-
- protected JDBCServices getJdbcServices() {
- return getConnectionProxy().getJdbcServices();
- }
-
- protected JDBCContainer getJdbcContainer() {
- return getConnectionProxy().getJdbcContainer();
- }
-
- public final Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- String methodName = method.getName();
- log.trace( "Handling invocation of statement method [{}]", methodName );
-
- if ( "toString".equals( methodName ) ) {
- return this.toString();
- }
-
- if ( "close".equals( methodName ) ) {
- explicitClose( ( Statement ) proxy );
- return null;
- }
-
- if ( "invalidate".equals( methodName ) ) {
- invalidateHandle();
- return null;
- }
-
- if ( "getWrappedStatement".equals( methodName ) ) {
- return getStatement();
- }
-
- if ( !valid ) {
- throw new HibernateException( "connection handle is no longer valid" );
- }
-
- beginningInvocationHandling( method, args );
-
- try {
- boolean exposingResultSet = doesMethodExposeResultSet( method );
-
- Object result = method.invoke( statement, args );
-
- if ( exposingResultSet ) {
- result = ProxyBuilder.buildResultSet( ( ResultSet ) result, this );
- getJdbcContainer().register( ( ResultSet ) result );
- }
-
- return result;
- }
- catch ( InvocationTargetException e ) {
- Throwable realException = e.getTargetException();
- if ( SQLException.class.isInstance( realException ) ) {
- throw connectionProxy.getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, "???", "???" );
- }
- else {
- throw realException;
- }
- }
- }
-
- protected void beginningInvocationHandling(Method method, Object[] args) {
- }
-
- private void explicitClose(Statement proxy) {
- if ( valid ) {
- getJdbcContainer().release( proxy );
- }
- }
-
- private void invalidateHandle() {
- connectionProxy = null;
- statement = null;
- valid = false;
- }
-
- protected boolean doesMethodExposeResultSet(Method method) {
- return ResultSet.class.isAssignableFrom( method.getReturnType() )
- && !method.getName().equals( "getGeneratedKeys" );
- }
-}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -29,6 +29,7 @@
import org.hibernate.HibernateException;
import org.hibernate.jdbc.JDBCContainer;
import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.impl.LogicalConnectionImplementor;
/**
* The InvocationHandler for intercepting messages to {@link Statement} proxies.
@@ -148,7 +149,9 @@
private void explicitClose(Statement proxy) {
if ( valid ) {
+ LogicalConnectionImplementor lc = getConnectionProxy().getLogicalConnection();
getJdbcContainer().release( proxy );
+ lc.afterStatementExecution();
}
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -189,7 +189,7 @@
/**
* {@inheritDoc}
*/
- public void physicalConnectionObtained() {
+ public void physicalConnectionObtained(Connection connection) {
}
/**
Deleted: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -1,122 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Steve Ebersole
- */
-package org.hibernate.jdbc.proxy;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.hibernate.HibernateException;
-import org.hibernate.jdbc.JDBCServices;
-import org.hibernate.jdbc.JDBCContainer;
-
-/**
- * ResultSetProxy implementation
- *
- * @author Steve Ebersole
- */
-public class ResultSetProxy implements InvocationHandler {
-
- private static final Logger log = LoggerFactory.getLogger( ResultSetProxy.class );
-
- private boolean valid = true;
- private ResultSet physicalResultSet;
- private AbstractStatementProxy associatedStatementProxy;
-
- public ResultSetProxy(ResultSet physicalResultSet, AbstractStatementProxy associatedStatementProxy) {
- this.physicalResultSet = physicalResultSet;
- this.associatedStatementProxy = associatedStatementProxy;
- associatedStatementProxy.getJdbcContainer().register( physicalResultSet );
- }
-
- protected ResultSet getResultSet() {
- errorIfInvalid();
- return physicalResultSet;
- }
-
- protected void errorIfInvalid() {
- if ( !valid ) {
- throw new HibernateException( "resultset handle is no longer valid" );
- }
- }
-
- protected AbstractStatementProxy getStatementProxy() {
- return associatedStatementProxy;
- }
-
- protected JDBCServices getJdbcServices() {
- return getStatementProxy().getJdbcServices();
- }
-
- protected JDBCContainer getJdbcContainer() {
- return getStatementProxy().getJdbcContainer();
- }
-
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- String methodName = method.getName();
- log.trace( "Handling invocation of resultset method [{}]", methodName );
-
- if ( "toString".equals( methodName ) ) {
- return this.toString();
- }
-
- if ( "close".equals( methodName ) ) {
- explicitClose( ( ResultSet ) proxy );
- return null;
- }
-
- if ( "invalidate".equals( methodName ) ) {
- invalidateHandle();
- return null;
- }
-
- if ( "getWrappedStatement".equals( methodName ) ) {
- return getResultSet();
- }
-
- errorIfInvalid();
-
- try {
- return method.invoke( physicalResultSet, args );
- }
- catch ( InvocationTargetException e ) {
- Throwable realException = e.getTargetException();
- if ( SQLException.class.isInstance( realException ) ) {
- throw getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, "???" );
- }
- else {
- throw realException;
- }
- }
- }
-
- private void explicitClose(ResultSet proxy) {
- if ( valid ) {
- associatedStatementProxy.getJdbcContainer().release( proxy );
- }
- }
-
- private void invalidateHandle() {
- associatedStatementProxy = null;
- physicalResultSet = null;
- valid = false;
- }
-}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -24,10 +24,12 @@
import org.hibernate.JDBCException;
import org.hibernate.exception.SQLExceptionConverter;
+import org.hibernate.exception.SQLStateConverter;
+import org.hibernate.exception.ViolatedConstraintNameExtracter;
import org.hibernate.util.StringHelper;
/**
- * ExceptionHelper implementation
+ * Helper for handling SQLExceptions in various manners.
*
* @author Steve Ebersole
*/
@@ -35,31 +37,83 @@
public static final String DEFAULT_EXCEPTION_MSG = "SQL Exception";
public static final String DEFAULT_WARNING_MSG = "SQL Warning";
+ public static final SQLExceptionConverter DEFAULT_CONVERTER = new SQLStateConverter(
+ new ViolatedConstraintNameExtracter() {
+ public String extractConstraintName(SQLException e) {
+ return null;
+ }
+ }
+ );
+
private static final Logger log = LoggerFactory.getLogger( ExceptionHelper.class );
private SQLExceptionConverter sqlExceptionConverter;
+ /**
+ * Create an exception helper with a default exception converter.
+ */
+ public ExceptionHelper() {
+ sqlExceptionConverter = DEFAULT_CONVERTER;
+ }
+
+ /**
+ * Create an exception helper with a specific exception converter.
+ *
+ * @param sqlExceptionConverter The exception converter to use.
+ */
public ExceptionHelper(SQLExceptionConverter sqlExceptionConverter) {
this.sqlExceptionConverter = sqlExceptionConverter;
}
+ /**
+ * Access the current exception converter being used internally.
+ *
+ * @return The current exception converter.
+ */
public SQLExceptionConverter getSqlExceptionConverter() {
return sqlExceptionConverter;
}
+ /**
+ * Inject the exception converter to use.
+ * <p/>
+ * NOTE : <tt>null</tt> is allowed and signifies to use the default.
+ *
+ * @param sqlExceptionConverter The converter to use.
+ */
public void setSqlExceptionConverter(SQLExceptionConverter sqlExceptionConverter) {
- this.sqlExceptionConverter = sqlExceptionConverter;
+ this.sqlExceptionConverter = ( sqlExceptionConverter == null ? DEFAULT_CONVERTER : sqlExceptionConverter );
}
+ /**
+ * Convert an SQLException using the current converter, doing some logging first.
+ *
+ * @param sqle The exception to convert
+ * @param message An error message.
+ * @return The converted exception
+ */
public JDBCException convert(SQLException sqle, String message) {
return convert( sqle, message, "n/a" );
}
+ /**
+ * Convert an SQLException using the current converter, doing some logging first.
+ *
+ * @param sqle The exception to convert
+ * @param message An error message.
+ * @param sql The SQL being executed when the exception occurred
+ * @return The converted exception
+ */
public JDBCException convert(SQLException sqle, String message, String sql) {
logExceptions( sqle, message + " [" + sql + "]" );
return sqlExceptionConverter.convert( sqle, message, sql );
}
+ /**
+ * Log any {@link SQLWarning}s registered with the connection.
+ *
+ * @param connection The connection to check for warnings.
+ */
public void logAndClearWarnings(Connection connection) {
if ( log.isWarnEnabled() ) {
try {
@@ -80,10 +134,21 @@
}
+ /**
+ * Log the given (and any nested) warning.
+ *
+ * @param warning The warning
+ */
public void logWarnings(SQLWarning warning) {
logWarnings( warning, null );
}
+ /**
+ * Log the given (and any nested) warning.
+ *
+ * @param warning The warning
+ * @param message The message text to use as a preamble.
+ */
public void logWarnings(SQLWarning warning, String message) {
if ( log.isWarnEnabled() ) {
if ( log.isDebugEnabled() && warning != null ) {
@@ -103,25 +168,36 @@
}
}
- public void logExceptions(SQLException ex) {
- logExceptions( ex, null );
+ /**
+ * Log the given (and any nested) exception.
+ *
+ * @param sqlException The exception to log
+ */
+ public void logExceptions(SQLException sqlException) {
+ logExceptions( sqlException, null );
}
- public void logExceptions(SQLException ex, String message) {
+ /**
+ * Log the given (and any nested) exception.
+ *
+ * @param sqlException The exception to log
+ * @param message The message text to use as a preamble.
+ */
+ public void logExceptions(SQLException sqlException, String message) {
if ( log.isErrorEnabled() ) {
if ( log.isDebugEnabled() ) {
message = StringHelper.isNotEmpty( message ) ? message : DEFAULT_EXCEPTION_MSG;
- log.debug( message, ex );
+ log.debug( message, sqlException );
}
- while ( ex != null ) {
+ while ( sqlException != null ) {
StringBuffer buf = new StringBuffer( 30 )
.append( "SQL Error: " )
- .append( ex.getErrorCode() )
+ .append( sqlException.getErrorCode() )
.append( ", SQLState: " )
- .append( ex.getSQLState() );
+ .append( sqlException.getSQLState() );
log.warn( buf.toString() );
- log.error( ex.getMessage() );
- ex = ex.getNextException();
+ log.error( sqlException.getMessage() );
+ sqlException = sqlException.getNextException();
}
}
}
Modified: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-16 06:38:43 UTC (rev 13925)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-16 08:13:46 UTC (rev 13926)
@@ -72,8 +72,10 @@
stmnt.execute( "drop table SANDBOX_JDBC_TST if exists" );
stmnt.execute( "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" );
assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
- logicalConnection.getJdbcContainer().releaseResources();
+ assertTrue( logicalConnection.isPhysicallyConnected() );
+ stmnt.close();
assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+ assertTrue( logicalConnection.isPhysicallyConnected() ); // after_transaction specified
PreparedStatement ps = proxiedConnection.prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
ps.setLong( 1, 1 );
More information about the hibernate-commits
mailing list