Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:18:57+00:00 2026-05-27T22:18:57+00:00

I have a JSF 2.0 page where the user logs in and he has

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:18:58+00:00Added an answer on May 27, 2026 at 10:18 pm

    You are reinventing the wheel. Please use container managed entity manager by injecting it with @PersistenceContext annotation and EJB for managing transactions. It will be far easier and less error prone.

    Here is a simple DAO:

    @Stateless
    public class UserDAO {
    
       @PersistenceContext
       private EntityManager entityManager;
    
       public void insertUser(User user) {
          entityManager.persist(user); 
       }
    }
    

    By default every method in the EJB is transactional. You can achieve fine-grained control using the @TransactionAttribute annotation. It is really simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an inputText and a commandButton in a JSF page. The user inputs
I have a JSF page on Websphere Process Server (on top of WAS 7)
In a create new user jsf page, I have a SelectOneMenu with a custom
I have the following JSF 2.1 page <h:selectOneRadio value=#{userBean.newUser}> <f:selectItem itemValue=0 itemLabel=new User />
I have a CommandButton on my JSF page. Can I redirect the user to
I have a JSF/Seam web app which has a page with a form which,
I have a JSF Page which gets few inputs from the User, I want
I have a JSF 2.0 page that uses Primefaces p:dialog component. The user can
Suppose i have JSF page <h:body> <h:form id=formID prependId=false> <h:outputText id=randomID value=#{randomNumber.random}/> <h:commandButton id=buttonID
I have a JSF page that is included in other JSF pages (basically a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.