Hibernate SVN: r12917 - tags.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-10 15:21:29 -0400 (Fri, 10 Aug 2007)
New Revision: 12917
Added:
tags/annotations_v3_3_0_GA/
Removed:
tags/annotations_3_3_0_GA/
Log:
typo in branch
Copied: tags/annotations_v3_3_0_GA (from rev 12916, tags/annotations_3_3_0_GA)
17 years, 4 months
Hibernate SVN: r12916 - tags.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-10 15:16:49 -0400 (Fri, 10 Aug 2007)
New Revision: 12916
Added:
tags/entitymanager_v3_3_0_GA/
Removed:
tags/entitymanager_3_3_0_GA/
Log:
typo in branch
Copied: tags/entitymanager_v3_3_0_GA (from rev 12915, tags/entitymanager_3_3_0_GA)
17 years, 4 months
Hibernate SVN: r12915 - in trunk/HibernateExt/entitymanager/src: test/org/hibernate/ejb/test/ejb3configuration and 4 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-10 09:35:22 -0400 (Fri, 10 Aug 2007)
New Revision: 12915
Added:
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/NoOpListener.java
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/UnversionedLock.java
Modified:
trunk/HibernateExt/entitymanager/src/test-resources/explicitpar/META-INF/persistence.xml
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/EventOverridingTest.java
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/emops/FlushTest.java
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/LockTest.java
trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java
Log:
More tests on explicit events
Added: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/NoOpListener.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/NoOpListener.java (rev 0)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/NoOpListener.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -0,0 +1,14 @@
+//$Id: $
+package org.hibernate.ejb.test;
+
+import org.hibernate.event.PreInsertEventListener;
+import org.hibernate.event.PreInsertEvent;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class NoOpListener implements PreInsertEventListener {
+ public boolean onPreInsert(PreInsertEvent event) {
+ return false;
+ }
+}
Modified: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -27,6 +27,8 @@
import org.hibernate.stat.Statistics;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.JDBCException;
+import org.hibernate.event.EventListeners;
+import org.hibernate.engine.SessionImplementor;
//$Id$
@@ -215,6 +217,19 @@
}
+ public void testListeners() throws Exception {
+ EntityManagerFactory emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() );
+ EntityManager em = emf.createEntityManager();
+ EventListeners eventListeners = ( (SessionImplementor) em.getDelegate() ).getListeners();
+ assertEquals(
+ "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work",
+ eventListeners.getPreInsertEventListeners().length,
+ eventListeners.getPreUpdateEventListeners().length + 1);
+
+ em.close();
+ emf.close();
+ }
+
// This test does not make sense anymore, validator being autoregistered at the HAN level
// public void testListenersOverridingCfgXmlPar() throws Exception {
// EntityManagerFactory emf = Persistence.createEntityManagerFactory( "cfgxmlpar", new HashMap() );
Modified: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/EventOverridingTest.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/EventOverridingTest.java 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/EventOverridingTest.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -36,7 +36,7 @@
public void testEventPerProperties() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() );
EntityManager em = emf.createEntityManager();
- assertEquals( "Only validator should be present", 1,
+ assertEquals( "Only validator and explicit NoOp should be present", 2,
( (SessionImplementor) em.getDelegate() ).getListeners().getPreInsertEventListeners().length);
em.close();
emf.close();
Modified: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/emops/FlushTest.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/emops/FlushTest.java 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/emops/FlushTest.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -107,7 +107,9 @@
List list = query.getResultList();
for (Object obj : list) {
if (obj instanceof Decorate) {
- founds.add((Decorate) obj);
+ Decorate decorate = (Decorate) obj;
+ founds.add( decorate );
+ decorate.getPet().getName(); //load
}
}
return founds;
Modified: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/LockTest.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/LockTest.java 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/LockTest.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -55,11 +55,33 @@
em.remove( lock );
em.getTransaction().commit();
}
+ em.close();
}
+ public void testLockWriteOnUnversioned() throws Exception {
+ UnversionedLock lock = new UnversionedLock();
+ lock.setName( "second" );
+ EntityManager em = factory.createEntityManager();
+ em.getTransaction().begin();
+ em.persist( lock );
+ em.getTransaction().commit();
+
+ em.getTransaction().begin();
+ lock = em.getReference( UnversionedLock.class, lock.getId() );
+ em.lock( lock, LockModeType.READ );
+ em.getTransaction().commit();
+
+ em.getTransaction().begin();
+ lock = em.getReference( UnversionedLock.class, lock.getId() );
+ em.remove( lock );
+ em.getTransaction().commit();
+ em.close();
+ }
+
public Class[] getAnnotatedClasses() {
return new Class[]{
- Lock.class
+ Lock.class,
+ UnversionedLock.class
};
}
}
Added: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/UnversionedLock.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/UnversionedLock.java (rev 0)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/lock/UnversionedLock.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -0,0 +1,34 @@
+//$Id: $
+package org.hibernate.ejb.test.lock;
+
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class UnversionedLock {
+ @Id
+ @GeneratedValue
+ private Integer id;
+ private String name;
+
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Modified: trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java 2007-08-10 13:35:22 UTC (rev 12915)
@@ -202,6 +202,25 @@
em.close();
}
+ public void testTransactionAndContains() throws Exception {
+ EntityManager em = factory.createEntityManager();
+ em.getTransaction().begin();
+ Book book = new Book();
+ book.name = "Java for Dummies";
+ em.persist( book );
+ em.getTransaction().commit();
+ em.close();
+ em = factory.createEntityManager();
+ em.getTransaction().begin();
+ List result = em.createQuery("select book from Book book where book.name = :title").
+ setParameter( "title", book.name ).getResultList();
+ assertEquals( "EntityManager.commit() should trigger a flush()", 1, result.size() );
+ assertTrue( em.contains( result.get( 0 ) ) );
+ em.getTransaction().commit();
+ assertTrue( em.contains( result.get( 0 ) ) );
+ em.close();
+ }
+
public void testRollbackOnlyOnPersistenceException() throws Exception {
Book book = new Book();
book.name = "Stolen keys";
Modified: trunk/HibernateExt/entitymanager/src/test-resources/explicitpar/META-INF/persistence.xml
===================================================================
--- trunk/HibernateExt/entitymanager/src/test-resources/explicitpar/META-INF/persistence.xml 2007-08-10 05:35:01 UTC (rev 12914)
+++ trunk/HibernateExt/entitymanager/src/test-resources/explicitpar/META-INF/persistence.xml 2007-08-10 13:35:22 UTC (rev 12915)
@@ -34,7 +34,7 @@
value="read-write, RegionName"/>
<!-- event overriding -->
- <property name="hibernate.ejb.event.pre-insert" value=""/>
+ <property name="hibernate.ejb.event.pre-insert" value="org.hibernate.ejb.test.NoOpListener"/>
<!-- remove JACC and validator -->
<!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file -->
17 years, 4 months
Hibernate SVN: r12914 - in sandbox/trunk/jdbc-proxy/src: main/java/org/hibernate/jdbc/impl and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-08-10 01:35:01 -0400 (Fri, 10 Aug 2007)
New Revision: 12914
Added:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.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/impl/BasicJDBCContainer.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties
Log:
more proxy-based jdbc interaction checkins
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-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -26,6 +26,7 @@
public interface JDBCContainer {
public void register(Statement statement);
public void release(Statement statement);
+
public void register(ResultSet resultSet);
public void release(ResultSet resultSet);
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java 2007-08-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -17,7 +17,6 @@
import java.sql.Statement;
import java.sql.ResultSet;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
@@ -80,10 +79,10 @@
registeredStatements.clear();
}
- protected final void close(Statement statement) {
+ protected void close(Statement statement) {
log.trace( "closing prepared statement [" + statement + "]" );
try {
- // if we are unable to "clean" the prepared statement,
+ // if we are unable to "clan" the prepared statement,
// we do not close it
try {
if ( statement.getMaxRows() != 0 ) {
@@ -105,7 +104,7 @@
}
}
- protected final void close(ResultSet resultSet) {
+ protected void close(ResultSet resultSet) {
log.trace( "closing result set [" + resultSet + "]" );
try {
resultSet.close();
Modified: 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-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -16,34 +16,48 @@
package org.hibernate.jdbc.proxy;
import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Proxy;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
import java.sql.Statement;
-import java.sql.SQLException;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.Set;
+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 final String sql;
- private final Statement statement;
- private final ConnectionProxy connectionProxy;
- private Set resultSets;
+ public static final Class[] PROXY_INTERFACES = new Class[] { Statement.class };
+ public static final Class[] PS_PROXY_INTERFACES = new Class[] { PreparedStatement.class };
+ public static final Class[] CS_PROXY_INTERFACES = new Class[] { CallableStatement.class };
- protected AbstractStatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
- this.sql = sql;
+ private static final Logger log = LoggerFactory.getLogger( AbstractStatementProxy.class );
+
+ private boolean valid = true;
+ private Statement statement;
+ private ConnectionProxy connectionProxy;
+ private Set resultSetProxies;
+
+ protected AbstractStatementProxy(Statement statement, ConnectionProxy connectionProxy) {
this.statement = statement;
this.connectionProxy = connectionProxy;
+ getJdbcContainer().register( statement );
}
- protected String getSql() {
- return sql;
- }
-
protected Statement getStatement() {
return statement;
}
@@ -52,20 +66,56 @@
return connectionProxy;
}
- protected Set getResultSets() {
- return resultSets;
+ protected Set getResultSetProxies() {
+ return resultSetProxies;
}
- protected Set getOrCreateResultSets() {
- if ( resultSets == null ) {
- resultSets = new HashSet();
+ protected JDBCServices getJdbcServices() {
+ return getConnectionProxy().getJdbcServices();
+ }
+
+ protected JDBCContainer getJdbcContainer() {
+ return getConnectionProxy().getJdbcContainer();
+ }
+
+ protected Set getOrCreateResultSetProxies() {
+ if ( resultSetProxies == null ) {
+ resultSetProxies = new HashSet();
}
- return resultSets;
+ return resultSetProxies;
}
- protected Object invokeOnStatement(Method method, Object[] args) throws Throwable {
+ 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 ( !valid ) {
+ throw new HibernateException( "connection handle is invalid" );
+ }
+
+ if ( "close".equals( methodName ) ) {
+ explicitClose( ( Statement ) proxy );
+ return null;
+ }
+
+ beginningInvocationHandling( method, args );
+
try {
- return method.invoke( statement, args );
+ boolean exposingResultSet = doesMethodExposeResultSet( method );
+
+ Object result = method.invoke( statement, args );
+
+ if ( exposingResultSet ) {
+ ResultSetProxy handler = new ResultSetProxy( ( ResultSet ) result, this );
+ result = Proxy.newProxyInstance(
+ getClass().getClassLoader(),
+ ResultSetProxy.PROXY_INTERFACES,
+ handler
+ );
+ getOrCreateResultSetProxies().add( result );
+ }
+
+ return result;
}
catch ( InvocationTargetException e ) {
Throwable realException = e.getTargetException();
@@ -78,16 +128,39 @@
}
}
- void close(Statement statement) {
-
+ protected void beginningInvocationHandling(Method method, Object[] args) {
}
- /**
- * Release resources associated with this statement.
- *
- * NOTE : package-protected
- */
- void release() {
- // todo : eventually need to iterate results and release them
+ private void explicitClose(Statement proxy) {
+ if ( resultSetProxies != null ) {
+ Iterator itr = resultSetProxies.iterator();
+ while ( itr.hasNext() ) {
+ final ResultSet resultSetProxy = ( ResultSet ) itr.next();
+ try {
+ resultSetProxy.close();
+ }
+ catch ( SQLException e ) {
+ // should never ever happen...
+ log.warn( "unhandled SQLException escaped proxy handling!", e );
+ // but the underlying resources should still get cleaned up when the logical connection closes...
+ }
+ itr.remove();
+ }
+ }
+ connectionProxy.getJdbcContainer().release( statement );
+ connectionProxy.proxiedStatementExplicitlyClosed( proxy );
+ statement = null;
+ connectionProxy = null;
+ resultSetProxies = null;
+ valid = false;
}
+
+ protected boolean doesMethodExposeResultSet(Method method) {
+ return ResultSet.class.isAssignableFrom( method.getReturnType() )
+ && !method.getName().equals( "getGeneratedKeys" );
+ }
+
+ /*package-protected*/ void proxiedResultSetExplicitlyClosed(ResultSet proxy) {
+ resultSetProxies.remove( proxy );
+ }
}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java 2007-08-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -18,13 +18,20 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.sql.Connection;
+import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.hibernate.HibernateException;
+import org.hibernate.jdbc.ConnectionWrapper;
import org.hibernate.jdbc.JDBCContainer;
import org.hibernate.jdbc.JDBCServices;
import org.hibernate.jdbc.Observer;
@@ -36,9 +43,13 @@
* @author Steve Ebersole
*/
public class ConnectionProxy implements InvocationHandler, Observer {
+ public static final Class[] PROXY_INTERFACES = new Class[] { Connection.class, ConnectionWrapper.class };
+
private static final Logger log = LoggerFactory.getLogger( ConnectionProxy.class );
+ private boolean valid = true;
private LogicalConnectionImplementor logicalConnection;
+ private HashSet statementProxies;
public ConnectionProxy(LogicalConnectionImplementor logicalConnection) {
this.logicalConnection = logicalConnection;
@@ -47,24 +58,50 @@
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
+ log.trace( "Handling invocation of Connection method [{}]", methodName );
+ if ( !valid ) {
+ throw new HibernateException( "connection handle is invalid" );
+ }
+
if ( "getWrappedConnection".equals( methodName ) ) {
return extractPhysicalConnection();
}
- // todo : close??? depends on whether we allow multiple connection proxies on the same logical connection
+ if ( "close".equals( methodName ) ) {
+ explicitClose();
+ return null;
+ }
try {
+ boolean creatingBasicStatement = "createStatement".equals( methodName );
+ boolean creatingPreparedStatement = "prepareStatement".equals( methodName );
+ boolean creatingCallableStatement = "prepareCall".equals( methodName );
+
Object result = method.invoke( extractPhysicalConnection(), args );
- if ( "prepareStatement".equals( methodName )
- || "prepareCall".equals( methodName )
- || "createStatement".equals( methodName ) ) {
- getJdbcContainer().register( ( Statement ) result ); // todo : this should eventually be the statemernt proxy
- if ( args != null && args.length > 0 ) {
- // there is a version of createStatement which does not take the SQL...
- logicalConnection.getJdbcServices().getSqlStatementLogger().logStatement( ( String ) args[0] );
+ if ( creatingBasicStatement || creatingPreparedStatement || creatingCallableStatement ) {
+ AbstractStatementProxy proxyHandler;
+ Class[] interfaces;
+ if ( creatingPreparedStatement ) {
+ proxyHandler = new PreparedStatementProxy( ( String ) args[0], ( PreparedStatement ) result, this );
+ interfaces = AbstractStatementProxy.PS_PROXY_INTERFACES;
}
+ else if ( creatingCallableStatement ) {
+ proxyHandler = new PreparedStatementProxy( ( String ) args[0], ( PreparedStatement ) result, this );
+ interfaces = AbstractStatementProxy.CS_PROXY_INTERFACES;
+ }
+ else {
+ proxyHandler = new StatementProxy( ( Statement ) result, this );
+ interfaces = AbstractStatementProxy.PROXY_INTERFACES;
+ }
+
+ result = Proxy.newProxyInstance(
+ getClass().getClassLoader(), // use our classloader
+ interfaces,
+ proxyHandler
+ );
+ getOrCreateStatementProxies().add( result );
}
return result;
@@ -80,10 +117,49 @@
}
}
+ private Set getOrCreateStatementProxies() {
+ if ( statementProxies == null ) {
+ statementProxies = new HashSet();
+ }
+ return statementProxies;
+ }
+
+ private void explicitClose() {
+ if ( statementProxies != null ) {
+ Iterator itr = statementProxies.iterator();
+ while ( itr.hasNext() ) {
+ final Statement proxy = ( Statement ) itr.next();
+ try {
+ proxy.close();
+ }
+ catch ( SQLException e ) {
+ // should never ever happen...
+ log.warn( "unhandled SQLException escaped proxy handling!", e );
+ // but the underlying resources should still get cleaned up when the logical connection closes...
+ }
+ }
+ }
+ invalidateHandle();
+ }
+
+ private void invalidateHandle() {
+ log.trace( "Invalidating connection handle" );
+ logicalConnection = null;
+ valid = false;
+ if ( statementProxies != null ) {
+ statementProxies.clear();
+ statementProxies = null;
+ }
+ }
+
private Connection extractPhysicalConnection() {
return logicalConnection.getConnection();
}
+ /*package-protected*/ void proxiedStatementExplicitlyClosed(Statement proxy) {
+ statementProxies.remove( proxy );
+ }
+
/*package-protected*/ JDBCServices getJdbcServices() {
return logicalConnection.getJdbcServices();
}
@@ -103,7 +179,7 @@
public void logicalConnectionClosed() {
log.info( "*** logical connection closed ***" );
+ invalidateHandle();
}
-
}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -0,0 +1,33 @@
+/*
+ * 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.sql.Statement;
+
+/**
+ * PreparedStatementProxy implementation
+ *
+ * @author Steve Ebersole
+ */
+public class PreparedStatementProxy extends AbstractStatementProxy {
+ private final String sql;
+
+ protected PreparedStatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
+ super( statement, connectionProxy );
+ connectionProxy.getJdbcServices().getSqlStatementLogger().logStatement( sql );
+ this.sql = sql;
+ }
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -0,0 +1,83 @@
+/*
+ * 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;
+
+/**
+ * ResultSetProxy implementation
+ *
+ * @author Steve Ebersole
+ */
+public class ResultSetProxy implements InvocationHandler {
+ public static final Class[] PROXY_INTERFACES = new Class[] { ResultSet.class };
+
+ 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 );
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+ log.trace( "Handling invocation of resultset method [{}]", methodName );
+
+ if ( !valid ) {
+ throw new HibernateException( "connection handle is invalid" );
+ }
+
+ if ( "close".equals( methodName ) ) {
+ explicitClose( ( ResultSet ) proxy );
+ return null;
+ }
+
+ try {
+ return method.invoke( physicalResultSet, args );
+ }
+ catch ( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw associatedStatementProxy.getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, "???", "???" );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ private void explicitClose(ResultSet proxy) {
+ associatedStatementProxy.getJdbcContainer().release( physicalResultSet );
+ associatedStatementProxy.proxiedResultSetExplicitlyClosed( proxy );
+ physicalResultSet = null;
+ associatedStatementProxy = null;
+ valid = false;
+ }
+}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java 2007-08-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -15,8 +15,8 @@
*/
package org.hibernate.jdbc.proxy;
+import java.sql.Statement;
import java.lang.reflect.Method;
-import java.sql.Statement;
/**
* StatementProxy implementation
@@ -24,11 +24,20 @@
* @author Steve Ebersole
*/
public class StatementProxy extends AbstractStatementProxy {
- protected StatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
- super( sql, statement, connectionProxy );
+ protected StatementProxy(Statement statement, ConnectionProxy connectionProxy) {
+ super( statement, connectionProxy );
}
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- return null;
+ protected void beginningInvocationHandling(Method method, Object[] args) {
+ if ( isExecution( method ) ) {
+ getJdbcServices().getSqlStatementLogger().logStatement( ( String ) args[0] );
+ }
}
+
+ private boolean isExecution(Method method) {
+ String methodName = method.getName();
+ return "execute".equals( methodName )
+ || "executeQuery".equals( methodName )
+ || "executeUpdate".equals( methodName );
+ }
}
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-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-10 05:35:01 UTC (rev 12914)
@@ -87,9 +87,8 @@
ps.setString( 2, "name" );
ps.execute();
-// statement and resultset proxying not yet implemented
-// ps = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
-// ps.executeQuery();
+ ps = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
+ ps.executeQuery();
assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
}
Modified: sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties 2007-08-09 20:10:00 UTC (rev 12913)
+++ sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties 2007-08-10 05:35:01 UTC (rev 12914)
@@ -18,7 +18,6 @@
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
-
log4j.rootLogger=info, stdout
log4j.logger.org.hibernate=trace
\ No newline at end of file
17 years, 4 months
Hibernate SVN: r12912 - trunk/HibernateExt/entitymanager/src/java/org/hibernate/ejb/packaging.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-09 15:56:35 -0400 (Thu, 09 Aug 2007)
New Revision: 12912
Modified:
trunk/HibernateExt/entitymanager/src/java/org/hibernate/ejb/packaging/JarVisitor.java
Log:
EJB-308 Container cl.getResources does not escape spaces properly
Modified: trunk/HibernateExt/entitymanager/src/java/org/hibernate/ejb/packaging/JarVisitor.java
===================================================================
--- trunk/HibernateExt/entitymanager/src/java/org/hibernate/ejb/packaging/JarVisitor.java 2007-08-09 19:54:17 UTC (rev 12911)
+++ trunk/HibernateExt/entitymanager/src/java/org/hibernate/ejb/packaging/JarVisitor.java 2007-08-09 19:56:35 UTC (rev 12912)
@@ -60,7 +60,7 @@
//Original URL is like jar:protocol
jarUrl = new URL( file );
if ( "file".equals( jarUrl.getProtocol() ) ) {
- //Do some voodoo magic because WAS doesn't think escaping URL is an interesting feature
+ //not escaped, need to voodoo
if ( file.indexOf( ' ' ) != -1 ) {
//not escaped, need to voodoo
jarUrl = new File( jarUrl.getFile() ).toURI().toURL(); //goes by toURI to escape the path
@@ -68,9 +68,9 @@
} //otherwise left as is
}
else if ( "zip".equals( protocol ) //Weblogic has it's own way
- || "code-source".equals( url.getProtocol() ) ) { //OC4J prevent ejb.jar access (ie everything without path)
+ || "code-source".equals( url.getProtocol() ) //OC4J prevent ejb.jar access (ie everything without path)
+ || "file".equals( protocol ) ) { //if no wrapping is done
//we have extracted the zip file, so it should be read as a file
- //Just in case some containers are as stupid as WAS: luckily WAS is closed source so nobody will dupe the bug
if ( file.indexOf( ' ' ) != -1 ) {
//not escaped, need to voodoo
jarUrl = new File(file).toURI().toURL(); //goes by toURI to escape the path
@@ -93,7 +93,7 @@
}
/**
- * Build a JarVisitor on the given JAR URL applying th given filters
+ * Build a JarVisitor on the given JAR URL applying the given filters
*
* @throws IllegalArgumentException if the URL is malformed
*/
17 years, 4 months
Hibernate SVN: r12911 - in sandbox/trunk: jdbc-proxy and 18 other directories.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-08-09 15:54:17 -0400 (Thu, 09 Aug 2007)
New Revision: 12911
Added:
sandbox/trunk/jdbc-proxy/
sandbox/trunk/jdbc-proxy/pom.xml
sandbox/trunk/jdbc-proxy/src/
sandbox/trunk/jdbc-proxy/src/main/
sandbox/trunk/jdbc-proxy/src/main/java/
sandbox/trunk/jdbc-proxy/src/main/java/org/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/ConnectionWrapper.java
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/LogicalConnection.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/Observer.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.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/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/SQLStatementLogger.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/Observer.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/ResourceTransaction.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/TransactionCoordinator.java
sandbox/trunk/jdbc-proxy/src/test/
sandbox/trunk/jdbc-proxy/src/test/java/
sandbox/trunk/jdbc-proxy/src/test/java/org/
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/BasicConnectionTests.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/TestingServiceImpl.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java
sandbox/trunk/jdbc-proxy/src/test/resources/
sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties
Log:
initial checkin of possible proxy-based jdbc interaction
Property changes on: sandbox/trunk/jdbc-proxy
___________________________________________________________________
Name: svn:ignore
+ target
test-output
local
*.ipr
*.iws
*.iml
.classpath
.project
.nbattrs
*.log
*.properties
.clover
Added: sandbox/trunk/jdbc-proxy/pom.xml
===================================================================
--- sandbox/trunk/jdbc-proxy/pom.xml (rev 0)
+++ sandbox/trunk/jdbc-proxy/pom.xml 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,73 @@
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-core-parent</artifactId>
+ <version>1</version>
+ </parent>
+
+ <groupId>org.hibernate.sandbox</groupId>
+ <artifactId>jdbc-proxy</artifactId>
+ <version>1</version>
+ <packaging>jar</packaging>
+
+ <name>JDBC Proxy (sandbox)</name>
+ <description>PoC of a proxy-based approach to JDBC interaction</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-core</artifactId>
+ <version>3.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.4.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.4.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.14</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>5.5</version>
+ <classifier>jdk15</classifier>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>1.8.0.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
+
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/ConnectionWrapper.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/ConnectionWrapper.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/ConnectionWrapper.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+import java.sql.Connection;
+
+/**
+ * Extra contract for {@link java.sql.Connection} proxies, for clients to be able to
+ * obtain the wrapped connection.
+ *
+ * @author Steve Ebersole
+ */
+public interface ConnectionWrapper {
+ /**
+ * Obtain the wrapped connection being delegated to.
+ * <p/>
+ * NOTE : The scope of validity for the returned connection is
+ * undefined.
+ *
+ * @return The wrapped connection.
+ */
+ public Connection getWrappedConnection();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainer.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+/**
+ * JDBCContainer contract
+ *
+ * @author Steve Ebersole
+ */
+public interface JDBCContainer {
+ public void register(Statement statement);
+ public void release(Statement statement);
+ public void register(ResultSet resultSet);
+ public void release(ResultSet resultSet);
+
+ public boolean hasRegisteredResources();
+
+ public void close();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCContainerBuilder.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Contract for building {@link JDBCContainer} instances.
+ * <p/>
+ * Used mainly to make explicit the contract that a
+ * {@link LogicalConnection} owns the lifecycle of the container.
+ *
+ * @author Steve Ebersole
+ */
+public interface JDBCContainerBuilder {
+ public JDBCContainer buildJdbcContainer();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/JDBCServices.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.jdbc.util.ExceptionHelper;
+import org.hibernate.jdbc.util.SQLStatementLogger;
+
+/**
+ * Contract for services around JDBC operations.
+ *
+ * @author Steve Ebersole
+ */
+public interface JDBCServices {
+ public ConnectionProvider getConnectionProvider();
+ public JDBCContainerBuilder getJdbcContainerBuilder();
+ public SQLStatementLogger getSqlStatementLogger();
+ public ExceptionHelper getExceptionHelper();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/LogicalConnection.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/LogicalConnection.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/LogicalConnection.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,62 @@
+/*
+ * 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;
+
+import java.sql.Connection;
+
+/**
+ * LogicalConnection contract
+ *
+ * @author Steve Ebersole
+ */
+public interface LogicalConnection {
+ /**
+ * Is this LogicalConnection open? Another phraseology sometimes used is: "are we
+ * logically connected"?
+ *
+ * @return True if physically connected; false otherwise.
+ */
+ public boolean isOpen();
+
+ /**
+ * Is this LogicalConnection instance "physically" connected. Meaning
+ * do we currently internally have a cached connection.
+ *
+ * @return True if physically connected; false otherwise.
+ */
+ public boolean isPhysicallyConnected();
+
+ /**
+ * Retreives the connection currently "logically" managed by this LogicalConnectionImpl.
+ * <p/>
+ * Note, that we may need to obtain a connection to return here if a
+ * connection has either not yet been obtained (non-UserSuppliedConnectionProvider)
+ * or has previously been aggressively released.
+ *
+ * @return The current Connection.
+ */
+ public Connection getConnection();
+
+ /**
+ * Release the underlying connection and clean up any other resources associated
+ * with this logical connection.
+ * <p/>
+ * This leaves the logical connection in a "no longer useable" state.
+ *
+ * @return The physical connection which was being used.
+ */
+ public Connection close();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/Observer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/Observer.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/Observer.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,27 @@
+/*
+ * 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;
+
+/**
+ * Observer contract
+ *
+ * @author Steve Ebersole
+ */
+public interface Observer {
+ public void physicalConnectionObtained();
+ public void physicalConnectionReleased();
+ public void logicalConnectionClosed();
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/BasicJDBCContainer.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,117 @@
+/*
+ * 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.impl;
+
+import java.sql.Statement;
+import java.sql.ResultSet;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.jdbc.JDBCContainer;
+
+/**
+ * BasicJDBCContainer implementation
+ *
+ * @author Steve Ebersole
+ */
+public class BasicJDBCContainer implements JDBCContainer {
+ private static final Logger log = LoggerFactory.getLogger( BasicJDBCContainer.class );
+
+ protected final HashSet registeredStatements = new HashSet();
+ protected final HashSet registeredResultSets = new HashSet();
+
+ public final void register(Statement statement) {
+ log.trace( "registering prepared statement [" + statement + "]" );
+ registeredStatements.add( statement );
+ }
+
+ public final void release(Statement statement) {
+ log.trace( "releasing prepared statement [" + statement + "]" );
+ registeredStatements.remove( statement );
+ close( statement );
+ }
+
+ public final void register(ResultSet resultSet) {
+ log.trace( "registering result set [" + resultSet + "]" );
+ registeredResultSets.add( resultSet );
+ }
+
+ public final void release(ResultSet resultSet) {
+ log.trace( "releasing result set [" + resultSet + "]" );
+ registeredResultSets.remove( resultSet );
+ close( resultSet );
+ }
+
+ public boolean hasRegisteredResources() {
+ return ! ( registeredStatements.isEmpty() && registeredResultSets.isEmpty() );
+ }
+
+ public void close() {
+ log.trace( "closing JDBC container [" + this + "]" );
+ Iterator iter;
+ iter = registeredResultSets.iterator();
+ while ( iter.hasNext() ) {
+ close( ( ResultSet ) iter.next() );
+ }
+ registeredResultSets.clear();
+
+ iter = registeredStatements.iterator();
+ while ( iter.hasNext() ) {
+ close( ( Statement ) iter.next() );
+ }
+ registeredStatements.clear();
+ }
+
+ protected final void close(Statement statement) {
+ log.trace( "closing prepared statement [" + statement + "]" );
+ try {
+ // if we are unable to "clean" the prepared statement,
+ // we do not close it
+ try {
+ if ( statement.getMaxRows() != 0 ) {
+ statement.setMaxRows( 0 );
+ }
+ if ( statement.getQueryTimeout() != 0 ) {
+ statement.setQueryTimeout( 0 );
+ }
+ }
+ catch( SQLException sqle ) {
+ // there was a problem "cleaning" the prepared statement
+ log.debug( "Exception clearing maxRows/queryTimeout [" + sqle.getMessage() + "]" );
+ return; // EARLY EXIT!!!
+ }
+ statement.close();
+ }
+ catch( SQLException sqle ) {
+ log.debug( "Unable to release statement [" + sqle.getMessage() + "]" );
+ }
+ }
+
+ protected final void close(ResultSet resultSet) {
+ log.trace( "closing result set [" + resultSet + "]" );
+ try {
+ resultSet.close();
+ }
+ catch( SQLException e ) {
+ log.debug( "Unable to release result set [" + e.getMessage() + "]" );
+ }
+ }
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImpl.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,223 @@
+/*
+ * 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.impl;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.ConnectionReleaseMode;
+import org.hibernate.HibernateException;
+import org.hibernate.JDBCException;
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.Observer;
+
+/**
+ * LogicalConnectionImpl implementation
+ *
+ * @author Steve Ebersole
+ */
+public class LogicalConnectionImpl implements LogicalConnectionImplementor {
+ private static final Logger log = LoggerFactory.getLogger( LogicalConnectionImpl.class );
+
+ private transient Connection physicalConnection;
+ private final ConnectionReleaseMode connectionReleaseMode;
+ private final JDBCServices jdbcServices;
+ private final JDBCContainer jdbcContainer;
+ private final List observers = new ArrayList();
+
+ private final boolean isUserSuppliedConnection;
+ private boolean isClosed;
+
+ public LogicalConnectionImpl(
+ Connection userSuppliedConnection,
+ ConnectionReleaseMode connectionReleaseMode,
+ JDBCServices jdbcServices) {
+ this.physicalConnection = userSuppliedConnection;
+ this.connectionReleaseMode = connectionReleaseMode;
+ this.jdbcServices = jdbcServices;
+ this.jdbcContainer = jdbcServices.getJdbcContainerBuilder().buildJdbcContainer();
+
+ this.isUserSuppliedConnection = ( userSuppliedConnection != null );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public JDBCServices getJdbcServices() {
+ return jdbcServices;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ConnectionReleaseMode getConnectionReleaseMode() {
+ return connectionReleaseMode;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public JDBCContainer getJdbcContainer() {
+ return jdbcContainer;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addObserver(Observer observer) {
+ observers.add( observer );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void afterStatement() {
+ // todo : implement...
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void afterTransaction() {
+ // todo : implement...
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isOpen() {
+ return !isClosed;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isPhysicallyConnected() {
+ return physicalConnection != null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Connection getConnection() throws HibernateException {
+ if ( isClosed ) {
+ throw new HibernateException( "Logical connection is closed" );
+ }
+ if ( physicalConnection == null ) {
+ if ( isUserSuppliedConnection ) {
+ // should never happen
+ throw new HibernateException( "User-supplied connection was null" );
+ }
+ obtainConnection();
+ }
+ return physicalConnection;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Connection close() {
+ log.trace( "closing logical connection" );
+ Connection c = physicalConnection;
+ if ( !isUserSuppliedConnection && physicalConnection != null ) {
+ releaseConnection();
+ }
+ // not matter what
+ physicalConnection = null;
+ isClosed = true;
+ jdbcContainer.close();
+ for ( Iterator itr = observers.iterator(); itr.hasNext(); ) {
+ ( ( Observer ) itr.next() ).logicalConnectionClosed();
+ }
+ log.trace( "logical connection closed" );
+ return c;
+ }
+
+// /**
+// * Is the connection considered "auto-commit"?
+// *
+// * @return True if we either do not have a connection, or the connection
+// * really is in auto-commit mode.
+// *
+// * @throws SQLException Can be thrown by the Connection.isAutoCommit() check.
+// */
+// public boolean isAutoCommit() throws SQLException {
+// return physicalConnection == null || physicalConnection.getAutoCommit();
+// }
+//
+// /**
+// * Force aggresive release of the underlying connection.
+// */
+// public void aggressiveRelease() {
+// if ( !isUserSuppliedConnection ) {
+// log.debug( "aggressively releasing JDBC connection" );
+// if ( physicalConnection != null ) {
+// releaseConnection();
+// }
+// }
+// }
+
+
+ /**
+ * Pysically opens a JDBC Connection.
+ *
+ * @throws JDBCException Indicates problem opening a connection
+ */
+ private void obtainConnection() throws JDBCException {
+ log.debug( "obtaining JDBC connection" );
+ try {
+ physicalConnection = getJdbcServices().getConnectionProvider().getConnection();
+ for ( Iterator itr = observers.iterator(); itr.hasNext(); ) {
+ ( ( Observer ) itr.next() ).physicalConnectionObtained();
+ }
+ log.debug( "obtained JDBC connection" );
+ }
+ catch (SQLException sqle) {
+ throw getJdbcServices().getExceptionHelper().convert( sqle, "Could not open connection" );
+ }
+ }
+
+ /**
+ * Physically closes the JDBC Connection.
+ *
+ * @throws JDBCException Indicates problem closing a connection
+ */
+ private void releaseConnection() throws JDBCException {
+ log.debug( "releasing JDBC connection" );
+ try {
+ if ( !physicalConnection.isClosed() ) {
+ getJdbcServices().getExceptionHelper().logAndClearWarnings( physicalConnection );
+ }
+ getJdbcServices().getConnectionProvider().closeConnection( physicalConnection );
+ physicalConnection = null;
+ for ( Iterator itr = observers.iterator(); itr.hasNext(); ) {
+ ( ( Observer ) itr.next() ).physicalConnectionReleased();
+ }
+ log.debug( "released JDBC connection" );
+ }
+ catch (SQLException sqle) {
+ throw getJdbcServices().getExceptionHelper().convert( sqle, "Could not close connection" );
+ }
+ }
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/impl/LogicalConnectionImplementor.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,42 @@
+/*
+ * 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.impl;
+
+import org.hibernate.ConnectionReleaseMode;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.LogicalConnection;
+import org.hibernate.jdbc.Observer;
+
+/**
+ * The "internal" contract for LogicalConnection
+ *
+ * @author Steve Ebersole
+ */
+public interface LogicalConnectionImplementor extends LogicalConnection {
+
+ public JDBCServices getJdbcServices();
+
+ public ConnectionReleaseMode getConnectionReleaseMode();
+
+ public JDBCContainer getJdbcContainer();
+
+ public void addObserver(Observer observer);
+
+ public void afterStatement();
+
+ public void afterTransaction();
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,93 @@
+/*
+ * 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.Statement;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * AbstractStatementProxy implementation
+ *
+ * @author Steve Ebersole
+ */
+public abstract class AbstractStatementProxy implements InvocationHandler {
+ private final String sql;
+ private final Statement statement;
+ private final ConnectionProxy connectionProxy;
+ private Set resultSets;
+
+ protected AbstractStatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
+ this.sql = sql;
+ this.statement = statement;
+ this.connectionProxy = connectionProxy;
+ }
+
+ protected String getSql() {
+ return sql;
+ }
+
+ protected Statement getStatement() {
+ return statement;
+ }
+
+ protected ConnectionProxy getConnectionProxy() {
+ return connectionProxy;
+ }
+
+ protected Set getResultSets() {
+ return resultSets;
+ }
+
+ protected Set getOrCreateResultSets() {
+ if ( resultSets == null ) {
+ resultSets = new HashSet();
+ }
+ return resultSets;
+ }
+
+ protected Object invokeOnStatement(Method method, Object[] args) throws Throwable {
+ try {
+ return method.invoke( statement, args );
+ }
+ catch ( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw connectionProxy.getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, "???", "???" );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ void close(Statement statement) {
+
+ }
+
+ /**
+ * Release resources associated with this statement.
+ *
+ * NOTE : package-protected
+ */
+ void release() {
+ // todo : eventually need to iterate results and release them
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,109 @@
+/*
+ * 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.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.Observer;
+import org.hibernate.jdbc.impl.LogicalConnectionImplementor;
+
+/**
+ * ConnectionProxy implementation
+ *
+ * @author Steve Ebersole
+ */
+public class ConnectionProxy implements InvocationHandler, Observer {
+ private static final Logger log = LoggerFactory.getLogger( ConnectionProxy.class );
+
+ private LogicalConnectionImplementor logicalConnection;
+
+ public ConnectionProxy(LogicalConnectionImplementor logicalConnection) {
+ this.logicalConnection = logicalConnection;
+ this.logicalConnection.addObserver( this );
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+
+ if ( "getWrappedConnection".equals( methodName ) ) {
+ return extractPhysicalConnection();
+ }
+
+ // todo : close??? depends on whether we allow multiple connection proxies on the same logical connection
+
+ try {
+ Object result = method.invoke( extractPhysicalConnection(), args );
+
+ if ( "prepareStatement".equals( methodName )
+ || "prepareCall".equals( methodName )
+ || "createStatement".equals( methodName ) ) {
+ getJdbcContainer().register( ( Statement ) result ); // todo : this should eventually be the statemernt proxy
+ if ( args != null && args.length > 0 ) {
+ // there is a version of createStatement which does not take the SQL...
+ logicalConnection.getJdbcServices().getSqlStatementLogger().logStatement( ( String ) args[0] );
+ }
+ }
+
+ return result;
+ }
+ catch( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw logicalConnection.getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, "???", "???" );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ private Connection extractPhysicalConnection() {
+ return logicalConnection.getConnection();
+ }
+
+ /*package-protected*/ JDBCServices getJdbcServices() {
+ return logicalConnection.getJdbcServices();
+ }
+
+ /*package-protected*/ JDBCContainer getJdbcContainer() {
+ return logicalConnection.getJdbcContainer();
+ }
+
+ // JDBC Observer impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ public void physicalConnectionObtained() {
+ }
+
+ public void physicalConnectionReleased() {
+ log.info( "logical connection releasing its physical connection");
+ }
+
+ public void logicalConnectionClosed() {
+ log.info( "*** logical connection closed ***" );
+ }
+
+
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,34 @@
+/*
+ * 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.Method;
+import java.sql.Statement;
+
+/**
+ * StatementProxy implementation
+ *
+ * @author Steve Ebersole
+ */
+public class StatementProxy extends AbstractStatementProxy {
+ protected StatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
+ super( sql, statement, connectionProxy );
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ return null;
+ }
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/ExceptionHelper.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,128 @@
+/*
+ * 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.util;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.JDBCException;
+import org.hibernate.exception.SQLExceptionConverter;
+import org.hibernate.util.StringHelper;
+
+/**
+ * ExceptionHelper implementation
+ *
+ * @author Steve Ebersole
+ */
+public class ExceptionHelper {
+ public static final String DEFAULT_EXCEPTION_MSG = "SQL Exception";
+ public static final String DEFAULT_WARNING_MSG = "SQL Warning";
+
+ private static final Logger log = LoggerFactory.getLogger( ExceptionHelper.class );
+
+ private SQLExceptionConverter sqlExceptionConverter;
+
+ public ExceptionHelper(SQLExceptionConverter sqlExceptionConverter) {
+ this.sqlExceptionConverter = sqlExceptionConverter;
+ }
+
+ public SQLExceptionConverter getSqlExceptionConverter() {
+ return sqlExceptionConverter;
+ }
+
+ public void setSqlExceptionConverter(SQLExceptionConverter sqlExceptionConverter) {
+ this.sqlExceptionConverter = sqlExceptionConverter;
+ }
+
+ public JDBCException convert(SQLException sqle, String message) {
+ return convert( sqle, message, "n/a" );
+ }
+
+ public JDBCException convert(SQLException sqle, String message, String sql) {
+ logExceptions( sqle, message + " [" + sql + "]" );
+ return sqlExceptionConverter.convert( sqle, message, sql );
+ }
+
+ public void logAndClearWarnings(Connection connection) {
+ if ( log.isWarnEnabled() ) {
+ try {
+ logWarnings( connection.getWarnings() );
+ }
+ catch ( SQLException sqle ) {
+ //workaround for WebLogic
+ log.debug( "could not log warnings", sqle );
+ }
+ }
+ try {
+ //Sybase fail if we don't do that, sigh...
+ connection.clearWarnings();
+ }
+ catch ( SQLException sqle ) {
+ log.debug( "could not clear warnings", sqle );
+ }
+
+ }
+
+ public void logWarnings(SQLWarning warning) {
+ logWarnings( warning, null );
+ }
+
+ public void logWarnings(SQLWarning warning, String message) {
+ if ( log.isWarnEnabled() ) {
+ if ( log.isDebugEnabled() && warning != null ) {
+ message = StringHelper.isNotEmpty( message ) ? message : DEFAULT_WARNING_MSG;
+ log.debug( message, warning );
+ }
+ while ( warning != null ) {
+ StringBuffer buf = new StringBuffer( 30 )
+ .append( "SQL Warning: " )
+ .append( warning.getErrorCode() )
+ .append( ", SQLState: " )
+ .append( warning.getSQLState() );
+ log.warn( buf.toString() );
+ log.warn( warning.getMessage() );
+ warning = warning.getNextWarning();
+ }
+ }
+ }
+
+ public void logExceptions(SQLException ex) {
+ logExceptions( ex, null );
+ }
+
+ public void logExceptions(SQLException ex, String message) {
+ if ( log.isErrorEnabled() ) {
+ if ( log.isDebugEnabled() ) {
+ message = StringHelper.isNotEmpty( message ) ? message : DEFAULT_EXCEPTION_MSG;
+ log.debug( message, ex );
+ }
+ while ( ex != null ) {
+ StringBuffer buf = new StringBuffer( 30 )
+ .append( "SQL Error: " )
+ .append( ex.getErrorCode() )
+ .append( ", SQLState: " )
+ .append( ex.getSQLState() );
+ log.warn( buf.toString() );
+ log.error( ex.getMessage() );
+ ex = ex.getNextException();
+ }
+ }
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/SQLStatementLogger.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/SQLStatementLogger.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/util/SQLStatementLogger.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,67 @@
+/*
+ * 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.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Centralize logging for SQL statements.
+ *
+ * @author Steve Ebersole
+ */
+public class SQLStatementLogger {
+ private static final Logger log = LoggerFactory.getLogger( SQLStatementLogger.class );
+
+ private boolean logToStdout;
+
+ /**
+ * Constructs a new SQLStatementLogger instance.
+ */
+ public SQLStatementLogger() {
+ this( false );
+ }
+
+ /**
+ * Constructs a new SQLStatementLogger instance.
+ *
+ * @param logToStdout Should we log to STDOUT in addition to our internal logger.
+ */
+ public SQLStatementLogger(boolean logToStdout) {
+ this.logToStdout = logToStdout;
+ }
+
+ /**
+ * Setter for property 'logToStdout'.
+ *
+ * @param logToStdout Value to set for property 'logToStdout'.
+ */
+ public void setLogToStdout(boolean logToStdout) {
+ this.logToStdout = logToStdout;
+ }
+
+ /**
+ * Log a SQL statement string.
+ *
+ * @param statement The SQL statement.
+ */
+ public void logStatement(String statement) {
+ log.debug( statement );
+ if ( logToStdout ) {
+ System.out.println( "Hibernate: " + statement );
+ }
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/Observer.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/Observer.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/Observer.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,27 @@
+/*
+ * 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.txn;
+
+/**
+ * Observer contract
+ *
+ * @author Steve Ebersole
+ */
+public interface Observer {
+ public void afterBegin();
+ public void beforeCompletion();
+ public void afterCompletion(boolean successful);
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/ResourceTransaction.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/ResourceTransaction.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/ResourceTransaction.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,24 @@
+/*
+ * 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.txn;
+
+/**
+ * ResourceTransaction contract
+ *
+ * @author Steve Ebersole
+ */
+public interface ResourceTransaction {
+}
Added: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/TransactionCoordinator.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/TransactionCoordinator.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/txn/TransactionCoordinator.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,37 @@
+/*
+ * 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.txn;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * TransactionCoordinator implementation
+ *
+ * @author Steve Ebersole
+ */
+public class TransactionCoordinator {
+ private ResourceTransaction resourceTransaction;
+ private final List observers = new ArrayList();
+
+ public void addObserver(Observer observer) {
+ observers.add( observer );
+ }
+
+ public void pulse() {
+ // attempt jta synch registration...
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+import java.util.Properties;
+
+import org.hibernate.cfg.Environment;
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.connection.DriverManagerConnectionProvider;
+
+/**
+ * ConnectionProviderBuilder implementation
+ *
+ * @author Steve Ebersole
+ */
+public class ConnectionProviderBuilder {
+ public static ConnectionProvider buildConnectionProvider() {
+ Properties props = new Properties( null );
+ props.put( Environment.DRIVER, "org.hsqldb.jdbcDriver" );
+ props.put( Environment.URL, "jdbc:hsqldb:mem:target/test/db/hsqldb/hibernate" );
+ props.put( Environment.USER, "sa" );
+ DriverManagerConnectionProvider connectionProvider = new DriverManagerConnectionProvider();
+ connectionProvider.configure( props );
+ return connectionProvider;
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/BasicConnectionTests.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/BasicConnectionTests.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/BasicConnectionTests.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,92 @@
+/*
+ * 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.impl;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.ConnectionReleaseMode;
+
+/**
+ * BasicConnectionTests implementation
+ *
+ * @author Steve Ebersole
+ */
+public class BasicConnectionTests {
+ private static final Logger log = LoggerFactory.getLogger( BasicConnectionTests.class );
+
+ private TestingServiceImpl services = new TestingServiceImpl();
+
+ @BeforeClass
+ protected void create() {
+ services.prepare();
+ }
+
+ @AfterClass
+ protected void destroy() {
+ services.release();
+ }
+
+ @Test
+ public void testBasicJdbcUsage() throws SQLException {
+ LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_TRANSACTION, services );
+ try {
+ try {
+ logicalConnection.getConnection().prepareStatement( "select count(*) from NON_EXISTENT" ).getResultSet();
+ }
+ catch ( SQLException e ) {
+ // expected outcome
+ }
+
+ Statement stmnt = logicalConnection.getConnection().createStatement();
+ logicalConnection.getJdbcContainer().register( stmnt );
+ 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().close();
+ assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+
+
+ PreparedStatement ps = logicalConnection.getConnection().prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
+ logicalConnection.getJdbcContainer().register( ps );
+ ps.setLong( 1, 1 );
+ ps.setString( 2, "name" );
+ ps.execute();
+
+ ps = logicalConnection.getConnection().prepareStatement( "select * from SANDBOX_JDBC_TST" );
+ logicalConnection.getJdbcContainer().register( ps );
+ ResultSet rs = ps.executeQuery();
+ logicalConnection.getJdbcContainer().register( rs );
+
+ assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+ }
+ finally {
+ logicalConnection.close();
+ }
+
+ assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/TestingServiceImpl.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/TestingServiceImpl.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/impl/TestingServiceImpl.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,79 @@
+/*
+ * 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.impl;
+
+import java.sql.SQLException;
+
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.JDBCContainerBuilder;
+import org.hibernate.jdbc.ConnectionProviderBuilder;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.util.SQLStatementLogger;
+import org.hibernate.jdbc.util.ExceptionHelper;
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.exception.SQLStateConverter;
+import org.hibernate.exception.ViolatedConstraintNameExtracter;
+
+/**
+ * TestingServiceImpl implementation
+ *
+ * @author Steve Ebersole
+ */
+public class TestingServiceImpl implements JDBCServices {
+ private ConnectionProvider connectionProvider;
+ private SQLStatementLogger sqlStatementLogger;
+ private JDBCContainerBuilder jdbcContainerBuilder;
+ private ExceptionHelper exceptionHelper;
+
+ public void prepare() {
+ connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
+ sqlStatementLogger = new SQLStatementLogger( true );
+ jdbcContainerBuilder = new JDBCContainerBuilder() {
+ public JDBCContainer buildJdbcContainer() {
+ return new BasicJDBCContainer();
+ }
+ };
+ exceptionHelper = new ExceptionHelper(
+ new SQLStateConverter(
+ new ViolatedConstraintNameExtracter() {
+ public String extractConstraintName(SQLException e) {
+ return null;
+ }
+ }
+ )
+ );
+ }
+
+ public void release() {
+ connectionProvider.close();
+ }
+
+ public ConnectionProvider getConnectionProvider() {
+ return connectionProvider;
+ }
+
+ public JDBCContainerBuilder getJdbcContainerBuilder() {
+ return jdbcContainerBuilder;
+ }
+
+ public SQLStatementLogger getSqlStatementLogger() {
+ return sqlStatementLogger;
+ }
+
+ public ExceptionHelper getExceptionHelper() {
+ return exceptionHelper;
+ }
+}
Added: 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 (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,105 @@
+/*
+ * 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.Proxy;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.ConnectionReleaseMode;
+import org.hibernate.JDBCException;
+import org.hibernate.jdbc.ConnectionWrapper;
+import org.hibernate.jdbc.impl.LogicalConnectionImpl;
+import org.hibernate.jdbc.impl.TestingServiceImpl;
+
+/**
+ * BasicConnectionProxyTest implementation
+ *
+ * @author Steve Ebersole
+ */
+public class BasicConnectionProxyTest {
+ private static final Logger log = LoggerFactory.getLogger( BasicConnectionProxyTest.class );
+
+ private org.hibernate.jdbc.impl.TestingServiceImpl services = new TestingServiceImpl();
+
+ @BeforeClass
+ protected void create() {
+ services.prepare();
+ }
+
+ @AfterClass
+ protected void destroy() {
+ services.release();
+ }
+
+ @Test
+ public void testBasicJdbcUsage() throws JDBCException {
+ LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_TRANSACTION, services );
+ ConnectionProxy proxyHandler = new ConnectionProxy( logicalConnection );
+ Connection proxiedConnection = ( Connection ) Proxy.newProxyInstance(
+ getClass().getClassLoader(),
+ new Class[] { Connection.class, ConnectionWrapper.class },
+ proxyHandler
+ );
+
+ try {
+ try {
+ proxiedConnection.prepareStatement( "select count(*) from NON_EXISTENT" ).executeQuery();
+ }
+ catch ( JDBCException ok ) {
+ // expected outcome
+ }
+
+ Statement stmnt = proxiedConnection.createStatement();
+ 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().close();
+ assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+
+ PreparedStatement ps = proxiedConnection.prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
+ ps.setLong( 1, 1 );
+ ps.setString( 2, "name" );
+ ps.execute();
+
+// statement and resultset proxying not yet implemented
+// ps = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
+// ps.executeQuery();
+
+ assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+ }
+ catch ( SQLException sqle ) {
+ fail( "incorrect exception type : sqlexception" );
+ }
+ finally {
+ logicalConnection.close();
+ }
+
+ assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+ }
+}
Added: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,81 @@
+/*
+ * 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.sql.SQLException;
+
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.JDBCContainerBuilder;
+import org.hibernate.jdbc.ConnectionProviderBuilder;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.impl.BasicJDBCContainer;
+import org.hibernate.jdbc.util.SQLStatementLogger;
+import org.hibernate.jdbc.util.ExceptionHelper;
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.exception.SQLStateConverter;
+import org.hibernate.exception.ViolatedConstraintNameExtracter;
+
+/**
+ * TestingServiceImpl implementation
+ *
+ * @author Steve Ebersole
+ */
+public class TestingServiceImpl implements JDBCServices {
+ private ConnectionProvider connectionProvider;
+ private SQLStatementLogger sqlStatementLogger;
+ private JDBCContainerBuilder jdbcContainerBuilder;
+ private ExceptionHelper exceptionHelper;
+
+ public void prepare() {
+ connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
+ sqlStatementLogger = new SQLStatementLogger( true );
+ jdbcContainerBuilder = new JDBCContainerBuilder() {
+ public JDBCContainer buildJdbcContainer() {
+ // todo : need to over ride this to handle statement/resultset proxies properly
+ return new BasicJDBCContainer();
+ }
+ };
+ exceptionHelper = new ExceptionHelper(
+ new SQLStateConverter(
+ new ViolatedConstraintNameExtracter() {
+ public String extractConstraintName(SQLException e) {
+ return null;
+ }
+ }
+ )
+ );
+ }
+
+ public void release() {
+ connectionProvider.close();
+ }
+
+ public ConnectionProvider getConnectionProvider() {
+ return connectionProvider;
+ }
+
+ public JDBCContainerBuilder getJdbcContainerBuilder() {
+ return jdbcContainerBuilder;
+ }
+
+ public SQLStatementLogger getSqlStatementLogger() {
+ return sqlStatementLogger;
+ }
+
+ public ExceptionHelper getExceptionHelper() {
+ return exceptionHelper;
+ }
+}
\ No newline at end of file
Added: sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/resources/log4j.properties 2007-08-09 19:54:17 UTC (rev 12911)
@@ -0,0 +1,24 @@
+################################################################################
+# 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 #
+################################################################################
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
+
+
+log4j.rootLogger=info, stdout
+
+log4j.logger.org.hibernate=trace
\ No newline at end of file
17 years, 4 months
Hibernate SVN: r12910 - in trunk/HibernateExt/annotations/src: java/org/hibernate/cfg/annotations and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-09 12:20:27 -0400 (Thu, 09 Aug 2007)
New Revision: 12910
Modified:
trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/SecondaryTableSecondPass.java
trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytomany/Cat.java
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/query/Chaos.java
Log:
ANN-648 handle joins table when dealing with comments and indexes
Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -773,8 +773,8 @@
mappings.addClass( persistentClass );
- mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder ) );
- //entityBinder.finalSecondaryTableBinding( propertyHolder );
+ //Process secondary tables and complementary definitions (ie o.h.a.Table)
+ mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, annotatedClass ) );
//add process complementary Table definition (index & all)
entityBinder.processComplementaryTableDefinitions( annotatedClass.getAnnotation( org.hibernate.annotations.Table.class ) );
Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/SecondaryTableSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/SecondaryTableSecondPass.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/SecondaryTableSecondPass.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -4,6 +4,7 @@
import java.util.Map;
import org.hibernate.MappingException;
+import org.hibernate.annotations.common.reflection.XAnnotatedElement;
import org.hibernate.cfg.annotations.EntityBinder;
/**
@@ -12,13 +13,16 @@
public class SecondaryTableSecondPass implements SecondPass {
private EntityBinder entityBinder;
private PropertyHolder propertyHolder;
+ private XAnnotatedElement annotatedClass;
- public SecondaryTableSecondPass(EntityBinder entityBinder, PropertyHolder propertyHolder) {
+ public SecondaryTableSecondPass(EntityBinder entityBinder, PropertyHolder propertyHolder, XAnnotatedElement annotatedClass) {
this.entityBinder = entityBinder;
this.propertyHolder = propertyHolder;
+ this.annotatedClass = annotatedClass;
}
public void doSecondPass(Map persistentClasses) throws MappingException {
entityBinder.finalSecondaryTableBinding( propertyHolder );
+
}
}
Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -740,19 +740,30 @@
}
public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) {
+ //comment and index are processed here
if ( table == null ) return;
String appliedTable = table.appliesTo();
Iterator tables = persistentClass.getTableClosureIterator();
Table hibTable = null;
while ( tables.hasNext() ) {
- hibTable = (Table) tables.next();
- if ( hibTable.getQuotedName().equals( appliedTable ) ) {
+ Table pcTable = (Table) tables.next();
+ if ( pcTable.getQuotedName().equals( appliedTable ) ) {
//we are in the correct table to find columns
+ hibTable = pcTable;
break;
}
hibTable = null;
}
if ( hibTable == null ) {
+ //maybe a join/secondary table
+ for ( Join join : secondaryTables.values() ) {
+ if ( join.getTable().getQuotedName().equals( appliedTable ) ) {
+ hibTable = join.getTable();
+ break;
+ }
+ }
+ }
+ if ( hibTable == null ) {
throw new AnnotationException(
"@org.hibernate.annotations.Table references an unknown table: " + appliedTable
);
Modified: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -197,10 +197,10 @@
*/
protected Class[] getMappings() {
return new Class[]{
- //Life.class,
- //Death.class,
- //Cat.class,
- //Dog.class,
+ Life.class,
+ Death.class,
+ Cat.class,
+ Dog.class,
A.class,
B.class,
C.class
Modified: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytomany/Cat.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytomany/Cat.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytomany/Cat.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -6,18 +6,26 @@
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
+import javax.persistence.JoinTable;
+import javax.persistence.JoinColumn;
+import org.hibernate.annotations.Index;
+
/**
* @author Emmanuel Bernard
*/
@Entity
@Table(name = "tbl_cat")
+//ANN-630
+//(a)org.hibernate.annotations.Table(appliesTo= "TT", indexes = @Index(name = "testidx", columnNames = "cat_id"))
public class Cat {
private CatPk id;
private int age;
private Set<Woman> humanContacts;
@ManyToMany
+ //@Index(name = "CAT_HUMAN_IDX")
+ @JoinTable(name="TT")
public Set<Woman> getHumanContacts() {
return humanContacts;
}
Modified: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/query/Chaos.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/query/Chaos.java 2007-08-09 15:01:14 UTC (rev 12909)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/query/Chaos.java 2007-08-09 16:20:27 UTC (rev 12910)
@@ -10,6 +10,7 @@
import javax.persistence.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
+import javax.persistence.Column;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
@@ -22,17 +23,18 @@
*/
@Entity
@Table(name="CHAOS")
-@SQLInsert( sql="INSERT INTO CHAOS(name, nickname, size, id) VALUES(upper(?),?,?,?)")
-@SQLUpdate( sql="UPDATE CHAOS SET name = upper(?), nickname = ?, size = ? WHERE id = ?")
+@SQLInsert( sql="INSERT INTO CHAOS(name, nick_name, size, id) VALUES(upper(?),?,?,?)")
+@SQLUpdate( sql="UPDATE CHAOS SET name = upper(?), nick_name = ?, size = ? WHERE id = ?")
@SQLDelete( sql="DELETE CHAOS WHERE id = ?")
@SQLDeleteAll( sql="DELETE CHAOS")
@Loader(namedQuery = "chaos")
-@NamedNativeQuery(name="chaos", query="select id, size, name, lower( nickname ) as nickname from CHAOS where id= ?", resultClass = Chaos.class)
+@NamedNativeQuery(name="chaos", query="select id, size, name, lower( nick_name ) as nick_name from CHAOS where id= ?", resultClass = Chaos.class)
public class Chaos {
@Id
private Long id;
private Long size;
private String name;
+ @Column(name="nick_name")
private String nickname;
@OneToMany
17 years, 4 months
Hibernate SVN: r12909 - trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-09 11:01:14 -0400 (Thu, 09 Aug 2007)
New Revision: 12909
Added:
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/A.java
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/B.java
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/C.java
Modified:
trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
Log:
More tests
Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/A.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/A.java (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/A.java 2007-08-09 15:01:14 UTC (rev 12909)
@@ -0,0 +1,24 @@
+//$Id$
+package org.hibernate.test.annotations.join;
+
+import java.util.Date;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.Column;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@MappedSuperclass
+public abstract class A {
+ @Column(nullable = false)
+ private Date createDate;
+
+
+ public Date getCreateDate() {
+ return createDate;
+ }
+
+ public void setCreateDate(Date createDate) {
+ this.createDate = createDate;
+ }
+}
Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/B.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/B.java (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/B.java 2007-08-09 15:01:14 UTC (rev 12909)
@@ -0,0 +1,38 @@
+//$Id$
+package org.hibernate.test.annotations.join;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
+public abstract class B extends A {
+ @Id
+ @GeneratedValue
+ private Integer id;
+ @Column(nullable = false) private String name;
+
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/C.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/C.java (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/C.java 2007-08-09 15:01:14 UTC (rev 12909)
@@ -0,0 +1,26 @@
+//$Id$
+package org.hibernate.test.annotations.join;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.SecondaryTable;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@DiscriminatorValue("C")
+@SecondaryTable(name="C")
+public class C extends B {
+ @Column(table = "C") private int age;
+
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+}
Modified: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java 2007-08-09 03:17:05 UTC (rev 12908)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java 2007-08-09 15:01:14 UTC (rev 12909)
@@ -175,16 +175,35 @@
s.close();
}
+ public void testMappedSuperclassAndSecondaryTable() throws Exception {
+ Session s = openSession( );
+ s.getTransaction().begin();
+ C c = new C();
+ c.setAge( 12 );
+ c.setCreateDate( new Date() );
+ c.setName( "Bob" );
+ s.persist( c );
+ s.flush();
+ s.clear();
+ c= (C) s.get( C.class, c.getId() );
+ assertNotNull( c.getCreateDate() );
+ assertNotNull( c.getName() );
+ s.getTransaction().rollback();
+ s.close();
+ }
+
/**
* @see org.hibernate.test.annotations.TestCase#getMappings()
*/
protected Class[] getMappings() {
return new Class[]{
- Life.class,
- Death.class,
- Cat.class,
- Dog.class
+ //Life.class,
+ //Death.class,
+ //Cat.class,
+ //Dog.class,
+ A.class,
+ B.class,
+ C.class
};
}
-
}
17 years, 4 months
Hibernate SVN: r12908 - in trunk/HibernateExt/search/src: test/org/hibernate/search/test/query and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-08-08 23:17:05 -0400 (Wed, 08 Aug 2007)
New Revision: 12908
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/query/ScrollableResultsImpl.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
Log:
HSEARCH-110 ScrollableResults handles out of bounds
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/query/ScrollableResultsImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/ScrollableResultsImpl.java 2007-08-08 16:17:19 UTC (rev 12907)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/ScrollableResultsImpl.java 2007-08-09 03:17:05 UTC (rev 12908)
@@ -6,18 +6,17 @@
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
+import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
-import java.util.List;
-import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.hibernate.HibernateException;
@@ -33,6 +32,19 @@
* Implements scollable and paginated resultsets.
* Contrary to query#iterate() or query#list(), this implementation is
* exposed to returned null objects (if the index is out of date).
+ * <p/>
+ * <p/>
+ * + * The following methods that change the value of 'current' will check
+ * + * and set its value to either 'afterLast' or 'beforeFirst' depending
+ * + * on direction. This is to prevent rogue values from setting it outside
+ * + * the boundaries of the results.
+ * + * <ul>
+ * + * <li>next()</li>
+ * + * <li>previous()</li>
+ * + * <li>scroll(i)</li>
+ * + * <li>last()</li>
+ * + * <li>first()</li>
+ * + * </ul>
*
* @author Emmanuel Bernard
* @author John Griffin
@@ -69,9 +81,9 @@
this.fetchSize = fetchSize;
}
- // The 'cache' is a sliding window that moves back and
- // forth over entityInfos loading values of size fetchSize
- // as necessary.
+ // The 'cache' is a sliding window of size fetchSize that
+ // moves back and forth over entityInfos as directed loading
+ // values as necessary.
private EntityInfo loadCache(int windowStart) {
int windowStop;
@@ -81,17 +93,17 @@
return info;
}
- if ( windowStart + fetchSize > max) {
+ if ( windowStart + fetchSize > max ) {
windowStop = max;
}
else {
windowStop = windowStart + fetchSize - 1;
}
- List<EntityInfo> entityInfosLoaded = new ArrayList<EntityInfo>(windowStop - windowStart + 1);
+ List<EntityInfo> entityInfosLoaded = new ArrayList<EntityInfo>( windowStop - windowStart + 1 );
for (int x = windowStart; x <= windowStop; x++) {
try {
- if (entityInfos[x - first] == null) {
+ if ( entityInfos[x - first] == null ) {
//FIXME should check that clazz match classes but this complicates a lot the firstResult/maxResult
entityInfos[x - first] = documentExtractor.extract( hits, x );
entityInfosLoaded.add( entityInfos[x - first] );
@@ -102,10 +114,10 @@
}
}
- //preload effitciently first
+ //preload efficiently first
loader.load( entityInfosLoaded.toArray( new EntityInfo[entityInfosLoaded.size()] ) );
//load one by one to inject null results if needed
- for ( EntityInfo slidingInfo : entityInfosLoaded ) {
+ for (EntityInfo slidingInfo : entityInfosLoaded) {
if ( !resultContext.containsKey( slidingInfo ) ) {
Object loaded = loader.load( slidingInfo );
if ( !loaded.getClass().isArray() ) loaded = new Object[] { loaded };
@@ -115,26 +127,77 @@
return entityInfos[windowStart - first];
}
+ /**
+ * Increases cursor pointer by one. If this places it >
+ * max + 1 (afterLast) then set it to afterLast and return
+ * false.
+ *
+ * @return booolean
+ * @throws HibernateException
+ */
public boolean next() throws HibernateException {
- return ++current <= max;
+ if ( ++current > max ) {
+ afterLast();
+ return false;
+ }
+ return true;
}
+ /**
+ * Decreases cursor pointer by one. If this places it <
+ * first - 1 (beforeFirst) then set it to beforeFirst and
+ * return false.
+ *
+ * @return boolean
+ * @throws HibernateException
+ */
public boolean previous() throws HibernateException {
- return --current >= first;
+ if ( --current < first ) {
+ beforeFirst();
+ return false;
+ }
+ return true;
}
+ /**
+ * Since we have to take into account that we can scroll any
+ * amount positive or negative, we perform the same tests that
+ * we performed in next() and previous().
+ *
+ * @param i
+ * @return boolean
+ * @throws HibernateException
+ */
public boolean scroll(int i) throws HibernateException {
current = current + i;
- return current >= first && current <= max;
+ if ( current > max ) {
+ afterLast();
+ return false;
+ }
+ else if ( current < first ) {
+ beforeFirst();
+ return false;
+ }
+ else {
+ return true;
+ }
}
public boolean last() throws HibernateException {
current = max;
+ if ( current < first ) {
+ beforeFirst();
+ return false;
+ }
return max >= first;
}
public boolean first() throws HibernateException {
current = first;
+ if ( current > max ) {
+ afterLast();
+ return false;
+ }
return max >= first;
}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-08-08 16:17:19 UTC (rev 12907)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-08-09 03:17:05 UTC (rev 12908)
@@ -368,15 +368,15 @@
result = results.get();
assertNull( result );
- // Let's see if a bad reverse scroll screws things up
- /**
- * These instructions uncover problems with calculations
- * of 'current'. It should be limited by first and max.
- */
- results.scroll( -6 );
+ results.scroll( -8 );
result = results.get();
assertNull( result );
+ // And test a bad forward scroll.
+ results.scroll( 10 );
+ result = results.get();
+ assertNull( result );
+
//cleanup
for (Object element : s.createQuery( "from " + Employee.class.getName() ).list()) s.delete( element );
tx.commit();
@@ -409,7 +409,58 @@
s.close();
}
+ public void testCurrent() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ prepEmployeeIndex( s );
+ s.clear();
+ Transaction tx = s.beginTransaction();
+ QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );
+
+ Query query = parser.parse( "dept:ITech" );
+ org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
+ hibQuery.setProjection( "id", "lastname", "dept" );
+
+
+
+ ScrollableResults results = hibQuery.scroll();
+ results.beforeFirst();
+ results.next();
+ assertTrue("beforeFirst() pointer incorrect", results.isFirst());
+
+ results.afterLast();
+ results.previous();
+ assertTrue("afterLast() pointer incorrect", results.isLast());
+
+ // Let's see if a bad reverse scroll screws things up
+ results.scroll( -8 );
+ results.next();
+ assertTrue("large negative scroll() pointer incorrect", results.isFirst());
+
+ // And test a bad forward scroll.
+ results.scroll( 10 );
+ results.previous();
+ assertTrue("large positive scroll() pointer incorrect", results.isLast());
+
+ // Finally, let's test a REAL screwup.
+ hibQuery.setFirstResult( 3 );
+ hibQuery.setMaxResults( 1 );
+
+ results = hibQuery.scroll();
+ results.first();
+ Object[] result = results.get();
+ assertEquals(1004, result[0]);
+
+ results.last();
+ result = results.get();
+ assertEquals(1004, result[0]);
+
+ //cleanup
+ for (Object element : s.createQuery( "from " + Employee.class.getName() ).list()) s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
public void testMultipleEntityPerIndex() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
17 years, 4 months
Hibernate SVN: r12907 - sandbox/trunk/jdbc.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-08-08 12:17:19 -0400 (Wed, 08 Aug 2007)
New Revision: 12907
Modified:
sandbox/trunk/jdbc/
Log:
added svn:ignore definition
Property changes on: sandbox/trunk/jdbc
___________________________________________________________________
Name: svn:ignore
+ target
local
*.ipr
*.iws
*.iml
.classpath
.project
.nbattrs
*.log
*.properties
.clover
17 years, 4 months