HHH-6147 - Add support for multi-tenancy to StatelessSession building

This commit is contained in:
Steve Ebersole 2011-05-04 11:02:01 -05:00
parent 33b33ca7dc
commit 8b709f7461
5 changed files with 108 additions and 2 deletions

View File

@ -118,6 +118,8 @@ public interface SessionBuilder {
* Define the tenant identifier to be associated with the opened session.
*
* @param tenantIdentifier The tenant identifier.
*
* @return {@code this}, for method chaining
*/
public SessionBuilder tenantIdentifier(String tenantIdentifier);
}

View File

@ -86,6 +86,13 @@ public interface SessionFactory extends Referenceable, Serializable {
*/
public Session getCurrentSession() throws HibernateException;
/**
* Obtain a {@link StatelessSession} builder.
*
* @return The stateless session builder
*/
public StatelessSessionBuilder withStatelessOptions();
/**
* Open a new stateless session.
*

View File

@ -0,0 +1,58 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate;
import java.sql.Connection;
/**
* Represents a consolidation of all stateless session creation options into a builder style delegate.
*
* @author Steve Ebersole
*/
public interface StatelessSessionBuilder {
/**
* Opens a session with the specified options.
*
* @return The session
*/
public StatelessSession openStatelessSession();
/**
* Adds a specific connection to the session options
*
* @param connection The connection to use.
*
* @return {@code this}, for method chaining
*/
public StatelessSessionBuilder connection(Connection connection);
/**
* Define the tenant identifier to be associated with the opened session.
*
* @param tenantIdentifier The tenant identifier.
*
* @return {@code this}, for method chaining
*/
public StatelessSessionBuilder tenantIdentifier(String tenantIdentifier);
}

View File

@ -63,6 +63,7 @@ import org.hibernate.SessionBuilder;
import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.StatelessSession;
import org.hibernate.StatelessSessionBuilder;
import org.hibernate.TypeHelper;
import org.hibernate.cache.CacheKey;
import org.hibernate.cache.CollectionRegion;
@ -524,12 +525,17 @@ public final class SessionFactoryImpl
return new SessionBuilderImpl( this );
}
@Override
public StatelessSessionBuilder withStatelessOptions() {
return new StatelessSessionBuilderImpl( this );
}
public StatelessSession openStatelessSession() {
return new StatelessSessionImpl( null, null, this );
return withStatelessOptions().openStatelessSession();
}
public StatelessSession openStatelessSession(Connection connection) {
return new StatelessSessionImpl( connection, null, this );
return withStatelessOptions().connection( connection ).openStatelessSession();
}
@Override
@ -1387,4 +1393,31 @@ public final class SessionFactoryImpl
return this;
}
}
public static class StatelessSessionBuilderImpl implements StatelessSessionBuilder {
private final SessionFactoryImpl sessionFactory;
private Connection connection;
private String tenantIdentifier;
public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public StatelessSession openStatelessSession() {
return new StatelessSessionImpl( connection, tenantIdentifier, sessionFactory );
}
@Override
public StatelessSessionBuilder connection(Connection connection) {
this.connection = connection;
return this;
}
@Override
public StatelessSessionBuilder tenantIdentifier(String tenantIdentifier) {
this.tenantIdentifier = tenantIdentifier;
return this;
}
}
}

View File

@ -38,6 +38,7 @@ import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.Cache;
import org.hibernate.HibernateException;
import org.hibernate.StatelessSessionBuilder;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.Session;
import org.hibernate.SessionBuilder;
@ -204,6 +205,11 @@ public class SessionFactoryStub implements SessionFactory {
return getImpl().getStatistics();
}
@Override
public StatelessSessionBuilder withStatelessOptions() {
return getImpl().withStatelessOptions();
}
public StatelessSession openStatelessSession() {
return getImpl().openStatelessSession();
}