diff --git a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java index d1dd117aaa..18bc7cc6b4 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java @@ -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); } diff --git a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java index 37324850b7..d95b41eaaa 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java @@ -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. * diff --git a/hibernate-core/src/main/java/org/hibernate/StatelessSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/StatelessSessionBuilder.java new file mode 100644 index 0000000000..5e735ef5b7 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/StatelessSessionBuilder.java @@ -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); +} diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 157221e782..fbde496e03 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -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; + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java b/hibernate-core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java index e5b701c6eb..aef87b6c0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java +++ b/hibernate-core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java @@ -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(); }