I have a JSF 2.0 page where the user logs in and he has a option to logout (surprisingly). My JBoss server configuration allows max 7 threads (connections). I test the page logging in several times with one user and after the 7th try I get Transaction not active which maybe means that after logout the connections don’t get back in the pool and stay open.
Q: What is the way to logout and return the thread in the thread pool? This question tortures me for a long time. Please help.
Here are the configuration in my JBoss standalone.xml for the datasource that limits the connections:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/MyJndiDS" pool-name="MyPoolDS" enabled="true" jta="true" use-java-context="false" use-ccm="true">
<connection-url>
jdbc:postgresql://192.168.2.125:5432/t_report
</connection-url>
<driver>
org.postgresql
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
3
</min-pool-size>
<max-pool-size>
7
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
my_user
</user-name>
<password>
my_pass
</password>
</security>
<statement>
<prepared-statement-cache-size>
32
</prepared-statement-cache-size>
</statement>
</datasource>
...
...
</datasources>
</subsystem>
and the logout method in a @SessionScoped class
import javax.faces.context.ExternalContext;
...
...
@Inject ExternalContext ec;
public void validateUserLogOut() {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
this.setUserLoggedIn(false);
navigation.logout();
}
EDIT: Here is how user logs in. Hope this helps.
public void validateLogUser() {
ResourceBundle bundle = ResourceBundle.getBundle("internationalization.language", context.getViewRoot().getLocale());
String validation = logUser();
if((validation == null) || validation.isEmpty()) {
context.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
bundle.getString("wrongUsername"),bundle.getString("wrongUsername")));
} else if (validation == "welcome") {
this.setUserLoggedIn(true);
navigation.login();
}
}
where the logUser() is:
public synchronized String logUser() {
try {
EntityManagerUtil.getEntityManager().getTransaction().begin();
System.out.println(user);
if(user.getUsername().isEmpty() || (user.getUsername() == null)) {
return null;
}
String password = user.getPassword();
user = (UserBean) EntityManagerUtil.getEntityManager().find(UserBean.class, user.getUsername());
if(user == null) {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
}
if(user.getPassword().equals(password)) {
log.info("User: " + user.getUsername() + " logged successfully.");
return "welcome";
} else {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
return null;
}
} catch (Exception e) {
log.error("Error while logging in : \n\t" + e);
EntityManagerUtil.getEntityManager().getTransaction().rollback();
return null;
} finally {
EntityManagerUtil.close();
}
}
and this is how EntityManagerUtil.getEntityManager() works:
/**
* ThreadLocal instance that holds unique EntityManager per thread,
* it means that every thread accessing this ThreadLocal will has it's own instance of EntityManager
*/
private static final ThreadLocal<EntityManager> entitymanager =
new ThreadLocal<EntityManager>();
/**
* @param persistenceUnit - String name of the persistece unit
* to be used as declared inside persistence.xml
* @return singleton instance of EntityManagerFactory
*/
public synchronized static EntityManagerFactory initializeEntityManagerFactory( String persistenceUnit ) {
if ( entityManagerFactory == null ) {
// Create the EntityManagerFactory
entityManagerFactory = Persistence.createEntityManagerFactory( persistenceUnit );
}
return entityManagerFactory;
}
/**
* @return Singleton instance of EntityManager per Thread
*/
public static EntityManager getEntityManager() {
initializeEntityManagerFactory( "MyPersistenceUnit" );
EntityManager entityManager = entitymanager.get();
// Create a new EntityManager
if ( entityManager == null) {
entityManager = entityManagerFactory.createEntityManager();
entitymanager.set( entityManager );
}
return entityManager;
}
/**
* Close all ThreadLocals
*/
public static void close() {
final EntityManager entityManager = entitymanager.get();
entitymanager.set( null );
if ( entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
if ( entityManagerFactory != null && entityManagerFactory.isOpen()) {
entityManagerFactory.close();
}
}
You are reinventing the wheel. Please use container managed entity manager by injecting it with
@PersistenceContextannotation andEJBfor managing transactions. It will be far easier and less error prone.Here is a simple DAO:
By default every method in the
EJBis transactional. You can achieve fine-grained control using the@TransactionAttributeannotation. It is really simple.