Vaadin 7 + Hibernate 4 + Spring 3: 2nd Factor

Hello!

My Vaadin application always returns the “Perm gen space (OutOfMemoryError)” error.

We tried solving this problem by increasing the physical memory (RAM) to higher values like this:

JAVAOPTIONS: -Xmx2048m -Xms1024m -XX:PermSize256m -XX:MaxPermSizeunderscore512m

but it doesn’t work.

Is there any other way to avoid that?

Thanks for advance! :slight_smile:

Best regards,

Maria.

Hi,

There are many factors that could cause an OutOfMemoryError. It could be too many concurrent users for your server capabilities, Hibernate queries that end up returning the whole (or almost the whole) database, or loading not-too-big datasets way too many times. In the end your code is trying to allocate memory that cannot fit into the heap.

Because with Vaadin you write web applications using only Java, sometimes it’s easy to forget that you are developing a web application that keeps each user’s UI state in memory. You should avoid loading big data sets and bind them to a Grid, for example. Instead use some kind of lazy-loading mechanism to retrieve data as needed.

I would suggest you investigate at which point in your code your application throws this error by inspecting the stack trace or using a tool such as VisualVM.

Hi Alejandro,

Thanks for your help!!! and sorry for the delay in my answer.

I revised the different issues that you comment.

1.- Concurrent users: When I’m doing the test only there are one user who is the user was logged in the web. There are no concurrency.

  1. -VisualVM: I can’t connect to my localhost from Netbeans :S

The application runs in “localhost:8200/name-web/jsp/login.jsp”

Whe run Maven I use: tomcat7:run-war
-Djava.rmi.server.hostname=localhost
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=8200

I try with localhost and 127.0.0.1 (http://stackoverflow.com/questions/10173834/java-rmi-djava-rmi-server-hostname-localhost-still-opens-a-socket-listening-on) but doesn’t work.

From VisualVM I indicated localhost:8200

When I try a connection and put the parameters and press ok, vm returns the message “cannot connect to localhost:8200 …”

3.- Hibernate Queries: At the moment I only search if user exist in database, if user doesn’t exist System create it. The function returns a user object .

Class Provider implements AuthenticationProvider {

@Autowired
private FilterDbUtil filterDbUtil;

@Override
public Authentication authenticate(Authentication authentication)
        throws AuthenticationException {

NEWCLASSAuthenticationDetails details =
(NEWCLASSAuthenticationDetails) authentication.getDetails();
String mCustomerId = details.getCustomerId();
String mUsername = (String) authentication.getPrincipal();
String mPassword = (String) authentication.getCredentials();
>>>>>> SmpUser smpUser = filterDbUtil.getUser(mCustomerId, mUsername);
}

public class FilterDbUtil {

            @Autowired
            private SessionFactory sessionFactory;

public SmpUser getUser(String customerId, String username) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String identifier = getUserIdentifier(customerId, username);
Query query = session.createQuery(“from ManageUser where identifier = :identifier”);
query.setParameter(“identifier”, identifier);
ManageUser manageUser = (ManageUser) query.uniqueResult();
tx.commit();

  if (manageUser == null) {
    tx = session.beginTransaction();
    manageUser = new ManageUser();
    manageUser.setIdentifier(identifier);
    saveUser(manageUser);
    tx.commit();
  }
  return manageUser;
} catch (HibernateException e) {
    session.getTransaction().rollback();
    return null;
} finally {
    if (session != null){
        session.close();
    }
}

}
public void saveUser(SmpUser user) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
e.printStackTrace();
}
} finally {
session.close();
}
}
}

@Entity
public class ManageUser {

            @Id
            @Column(name = "USER_NAME")
            private String identifier;
            …..

}

Application.spring.xml

<!-- Data Access Objects -->
<bean id="filterDbUtil" class="………….db.FilterDbUtil" />

<!-- Hibernate settings -->
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.user}" />
  <property name="password" value="${jdbc.pass}" />
</bean>

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="………..db.entity" />
  <property name="hibernateProperties">
     <props>
       
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>  

        <prop key="hibernate.cache.use_query_cache">false</prop>                             
        <prop key="hibernate.transaction.flush_before_completion">true</prop>                                   
       
        <!-- To show sql output on the screen -->
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>           
                   
        <!-- Connection pool C3P0 -->
        <prop key="hibernate.c3p0.acquire_increment">2</prop>
        <prop key="hibernate.c3p0.min_size">5</prop>
        <prop key="hibernate.c3p0.max_size">100</prop>
        <prop key="hibernate.c3p0.timeout">20</prop>
        <prop key="hibernate.c3p0.max_statements">50</prop>
        <prop key="hibernate.c3p0.idle_test_period">60</prop>
       
     </props>
  </property>
</bean>

Hibernate.properties

jdbc.X

jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
jdbc.url=jdbc:derby:${catalina.home}/FilterDB;create=true
jdbc.user=
jdbc.pass=

hibernate.X

hibernate.dialect=com…db.FixedDerbyDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

Thanks for advance!!!