Home  >  Hibernate 4

Hibernate 4 SharedSessionBuilder and Session.sessionWithOptions() Example

By Arvind Rai, February 13, 2015
In this page we will learn Hibernate 4 SharedSessionBuilder and Session.sessionWithOptions(). SharedSessionBuilder is instantiated using Session.sessionWithOptions(). Then SharedSessionBuilder is configured using its different methods and then calling SharedSessionBuilder.openSession() we get another session. This another session will behave as SharedSessionBuilder is configured. For the example, we can get a session, which will automatically be closed or a session which can join ongoing JTA transaction or a session which can use different connection as provided. Find the code snippet.
SharedSessionBuilder builder = session1.sessionWithOptions();
builder.autoClose(true);
Session session2 = builder.openSession(); 
org.hibernate.SharedSessionBuilder is created using sessionWithOptions() method of a Session. SharedSessionBuilder has different methods that can be configured for a Session. Some methods of SharedSessionBuilder are as below.

autoClose(boolean): If true, then just after transaction complete, session will automatically be closed.
autoJoinTransactions(boolean): If true, session will automatically join ongoing JTA transaction.
connection(Connection): Session will use the given connection.

Complete Example

Find the complete example where we will show a demo for auto close session. Just after transaction completion, session will automatically be closed.

Main Class Using SharedSessionBuilder.autoClose()

In this class, first we will get a session and using this session we will create SharedSessionBuilder instance. SharedSessionBuilder instance will be configured for auto close session just after transaction completion. For the example, we will commit the transaction and session will automatically be closed. And then if we access object from the session, we will get exception.
SharedSessionBuilderDemo.java
package com.concretepage;
import org.hibernate.Session;
import org.hibernate.SharedSessionBuilder;
public class SharedSessionBuilderDemo {
	public static void main(String[] args) {
		Session session1 = HibernateUtil.getSessionFactory().openSession();
		//Instantiate SharedSessionBuilder
		SharedSessionBuilder builder = session1.sessionWithOptions();
		//Configure SharedSessionBuilder for auto close session
		builder.autoClose(true);
		//Get another Session 
		Session session2 = builder.openSession();
		session2.beginTransaction();
		Student s = (Student) session2.load(Student.class, new Integer(1));
		System.out.println(s.getName());
		//Session will automatically be closed here.
		session2.getTransaction().commit();
		//Will throw exception as session is closed.
		s = (Student) session2.load(Student.class, new Integer(1));
		System.out.println(s.getName());
	}
} 

Entity

Find the entity being used in the example.
package com.concretepage;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
	@Id
	@Column(name="id")
	private int id;
	@Column(name="age")
	private int age;
	@Column(name="name")
	private String name;
	public Student(){}
	public Student(int id, int age, String name){
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
} 

Output

Find the output.
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
Rahim
Exception in thread "main" org.hibernate.SessionException: Session is closed! 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS



Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us