JSF App slow with JPA connection

If you are working on Java Persistence API JPA on tomcat or any other web server this would be happening if you have multiple threads going off for connections.

The rule of thumb shall be to have one EntityManagerFactory and get EntityManagers out of it. Hence we would have one factory but multiple products that would take care of closing and managing them selves.

What are the signs:

1. Do you instantiate Persistence.createEntityManagerFactory(“name”) from multiple places?
2. What do you see on Process when you run

ps -aux | grep tomcat

Do you see multiple instances

If either or both of the above have yes, then here is the solution.

The first thing have single instance of ManagerFactory


package com.enderase.persistence;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* Singlton implementation for EntityManagerFactory
*
* @author Kaleb Woldearegay<kaleb@gullele.com>
*/
public class HibernateUtil {
private static final EntityManagerFactory entityManagerFactory;

static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
} catch (Throwable exception) {
//log your error here
throw new ExceptionInInitializerError(exception);
}
}

public static EntityManagerFactory getEntityManager() {
return entityManagerFactory;
}
}

Then make sure you are taking care of the instances of the EntityManagers that are created from the factory using

EntityManagerFactory entityManagerFactory = HibernateUtil.getEntityManager();
EntityManager em = entityManagerFactory.createEntityManager();

Make sure to close them appropriately after using them

This should pretty much take care of the problem

 

Adding session bean to to requested bean using annotation JSF

One major part on JSF would separation of concerns even for beans. As a rule of thumb beans related to model are session beans and those which have actions to be taken care of are requested one.

So, In this particular scenario we would have two beans. Basically we don’t want to include any logic inside the session bean, rather we would add session bean as a member variable to request bean.

Lets take a simple registration process.

The requested bean which will be responsible for actions would look like

package com.enderase.beans;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.enderase.model.Contractor;

@ManagedBean
@RequestScoped
public class ActionListeners implements Serializable{
	
	@ManagedProperty(value="#{contractorBean.contractor}")
	private Contractor contractor;
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
	
	public Contractor getContractor() {
		return this.contractor;
	}
	
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * Action handler for Contractor save.
	 * @return String, next
	 */
	public String registerContractor() {
		Contractor contractor = this.contractor;
		if (contractor != null) {
			try {
				FileWriter fileWriter = new FileWriter("/tmp/name.note");
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write("Name "+contractor.getName()+" Email "+contractor.getEmail()
						+contractor.getState());
				bufferedWriter.close();
			} catch (Exception ex) {
				//log the exception here
			}
		}
		return "navigated";
	}
}

So the key thing here would be the @ManagedProperty part.
That would inject the session bean into the request bean without creating any instance of it.

*Don’t for get to add getter and setter for the session bean you are adding otherwise you would get an error.

The session bean would be a simple holder of model

package com.enderase.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.enderase.model.Contractor;

@ManagedBean
@SessionScoped
public class ContractorBean {
	
	private Contractor contractor;
	
	public ContractorBean() {
		this.contractor = new Contractor();
	}
	public Contractor getContractor(){
		return this.contractor;
	}
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
}

Where the contractor would be a simple POJO file

Could not open Hibernate Session for transaction

This problem happens when the connection to the database is disconnected or was not created at all by the application, but pool assumes connection is established.
Remedy for this could be checking credential to the database [username, password, host, port] and also checking the connection object before proceeding to database tasks like reading or updating.