Results 1 to 4 of 4
  1. #1
    andrissig is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Performance with JSF + Glassfish + PostgreSQL

    On one computer I have Glassfish server and PostreSQL DB.

    So I use JavaServer Faces. When my facelet page is without any connection to db, then page loads very fast.

    But when I use db connection (very simple, for example, select all rows from tabele with 5 rows), page loads 3-5 seconds.

    Where can be problem? Why working with db is so slow?

    Directly in postrgers(pgAdmin) query executes on ~10ms.

    My persistence.xml:

    XML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    
        <persistence-unit name="NewPersistenceUnit">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>org.test.majas.model.ForumPostsEntity</class>
            <class>org.test.majas.model.ForumsEntity</class>
            <class>org.test.majas.model.UsersEntity</class>
            <class>org.test.majas.model.SystemEntity</class>
            <class>org.test.majas.model.MenuitemEntity</class>
            <class>org.test.majas.model.MenusEntity</class>
            <class>org.test.majas.model.ProjectsEntity</class>
            <class>org.test.majas.model.UsersprojectsEntity</class>
            <properties>
                <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/majas"/>
                <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
                <property name="hibernate.connection.username" value="postgres"/>
                <property name="hibernate.connection.password" value="123"/>
                <property name="hibernate.archive.autodetection" value="class"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true"/>
                <property name="hbm2ddl.auto" value="update"/>
            </properties>
        </persistence-unit>
    </persistence>
    GenericDaoImpl:

    Java Code:
    public class GenericDaoImpl<T, PK extends Serializable>
            implements GenericDao<T, PK> {
    
        protected EntityManager entityManager;
        protected EntityManagerFactory emf;
    
        private Class<T> type;
    
        public GenericDaoImpl(Class<T> type) {
    
            emf = javax.persistence.Persistence.createEntityManagerFactory("NewPersistenceUnit");
            this.type = type;
        }
    
        public T create(T o) {
            this.entityManager = emf.createEntityManager();
            this.entityManager.getTransaction().begin();
            this.entityManager.persist(o);
            this.entityManager.getTransaction().commit();
            return o;
        }
    
        public T read(PK id) {
            this.entityManager = emf.createEntityManager();
            this.entityManager.getTransaction().begin();
            return this.entityManager.find(type, id);
        }
    
        public T update(T o) {
            this.entityManager = emf.createEntityManager();
            this.entityManager.getTransaction().begin();
            T rez = this.entityManager.merge(o);
            this.entityManager.getTransaction().commit();
            return rez;
        }
    
        public void delete(T o) {
            this.entityManager = emf.createEntityManager();
            this.entityManager.getTransaction().begin();
            o = this.entityManager.merge(o);
            this.entityManager.remove(o);
            this.entityManager.getTransaction().commit();
        }
    
    }
    For example, in MenusDao I have this function:

    Java Code:
        public MenusEntity getMenuByName(String name){
            this.entityManager = emf.createEntityManager();
            this.entityManager.getTransaction().begin();
            Query query = entityManager.createQuery("SELECT u FROM MenusEntity u where u.name = ?1");
            query.setParameter(1, name);
            List<MenusEntity> l = query.getResultList();
            if(l.size()>0){
                return l.get(0);
            }
            else{
                return null;
            }
        }
    When I load page which loads this function, then page loads on 3-4 seconds.
    Thanks!

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Performance with JSF + Glassfish + PostgreSQL

    "Page load" is very broad, you have to narrow down which part of the application is responsible for the slow down. My primary target is that JPA code that you added; see how long it takes to execute that query (I would just dumbly do two calls to System.currentTimeInMillis() and see what the difference is between the two calls). Probably that is where most of the time is going, and then it becomes the question: why? That is going to be a toughy to answer.

    Currently you have Hibernate setup to manage database connections itself; you may want to try to employ a datasource with connection pool to manage them for you so you don't have connection creation overhead.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    andrissig is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Performance with JSF + Glassfish + PostgreSQL

    So, diference between two calls of System.currentTimeMillis() is very small (40-70ms)... But in log file on every page load I see ~5 blocks of similar logtext:

    Java Code:
    [2014-03-03T23:39:57.268-0800] [glassfish 4.0] [INFO] [] [org.hibernate.jpa.internal.util.LogHelper] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797268] [levelValue: 800] [[
      HHH000204: Processing PersistenceUnitInfo [
    	name: NewPersistenceUnit
    	...]]]
    
    [2014-03-03T23:39:57.308-0800] [glassfish 4.0] [WARN] [] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797308] [levelValue: 900] [[
      HHH000402: Using Hibernate built-in connection pool (not for production use!)]]
    
    [2014-03-03T23:39:57.308-0800] [glassfish 4.0] [INFO] [] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797308] [levelValue: 800] [[
      HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/majas]]]
    
    [2014-03-03T23:39:57.318-0800] [glassfish 4.0] [INFO] [] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797318] [levelValue: 800] [[
      HHH000046: Connection properties: {user=postgres, password=****}]]
    
    [2014-03-03T23:39:57.318-0800] [glassfish 4.0] [INFO] [] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797318] [levelValue: 800] [[
      HHH000006: Autocommit mode: false]]
    
    [2014-03-03T23:39:57.318-0800] [glassfish 4.0] [INFO] [] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797318] [levelValue: 800] [[
      HHH000115: Hibernate connection pool size: 20 (min=1)]]
    
    [2014-03-03T23:39:57.709-0800] [glassfish 4.0] [INFO] [] [org.hibernate.dialect.Dialect] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797709] [levelValue: 800] [[
      HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect]]
    
    [2014-03-03T23:39:57.709-0800] [glassfish 4.0] [INFO] [] [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797709] [levelValue: 800] [[
      HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException]]
    
    [2014-03-03T23:39:57.729-0800] [glassfish 4.0] [INFO] [] [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797729] [levelValue: 800] [[
      HHH000397: Using ASTQueryTranslatorFactory]]
    
    [2014-03-03T23:39:57.929-0800] [glassfish 4.0] [WARN] [] [org.hibernate.jpa.internal.EntityManagerFactoryRegistry] [tid: _ThreadID=20 _ThreadName=http-listener-1(3)] [timeMillis: 1393918797929] [levelValue: 900] [[
      HHH000436: Entity manager factory name (NewPersistenceUnit) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name']]
    Each of that block takes ~500ms, I think there is the problem... How can I fix this? Thanks.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Performance with JSF + Glassfish + PostgreSQL

    Well I suggest you Google different parts of that warning you are getting and find out. You are likely not the first one to run into this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Show data from PostgreSQL
    By kike_skate in forum New To Java
    Replies: 5
    Last Post: 05-18-2011, 08:06 PM
  2. help with jdbc for postgresql
    By arano86 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 12:27 PM
  3. Error connecting to postgresql
    By murphyjirus in forum Advanced Java
    Replies: 6
    Last Post: 10-31-2008, 09:52 AM
  4. PostgreSQL driver
    By bugger in forum JDBC
    Replies: 3
    Last Post: 01-11-2008, 09:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •