Dear all,
i am getting memory leak for the lazy query container.
When ever i open new session , the db connection is getting created in the pool and not getting released .
Afterreaching the maximimum pool size , application crashes…
How to fix this issue:
My Code :
package com.duc.epi.view;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.vaadin.server.VaadinSession;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.addons.lazyquerycontainer.LazyEntityContainer;
import org.vaadin.addons.lazyquerycontainer.LazyQueryView;
import org.vaadin.addons.lazyquerycontainer.QueryItemStatus;
import com.duc.epi.dao.AuditDao;
import com.duc.epi.domain.PayAuthorize;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.filter.Compare;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.Responsive;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import ru.xpoft.vaadin.VaadinView;
@VaadinView(Transaction.NAME)
public class Transaction extends CustomComponent implements View , ClickListener{
private static final long serialVersionUID = -5676375900405316165L;
public static final String NAME = "Transaction";
@AutoGenerated
private AbsoluteLayout mainLayout;
@AutoGenerated
private VerticalLayout verticalLayout_1;
@AutoGenerated
private Table table_1;
@AutoGenerated
private AbsoluteLayout absoluteLayout_2;
private ComboBox box;
private Button applyFilter;
private TextField field ;
private Button remFilter;
private final EntityManagerFactory entityManagerFactory = (EntityManagerFactory) ((ApplicationContext)VaadinSession.getCurrent().getAttribute("CTX")).getBean("entityManagerFactory");
public final String PERSISTENCE_UNIT = "myPersistenceUnit";
private LazyEntityContainer<PayAuthorize> entityContainer;
/**
* The constructor should first build the main layout, set the
* composition root and then do any custom initialization.
*
* The constructor will not be automatically regenerated by the
* visual editor.
*/
public Transaction() {
addStyleName("transactions");
buildMainLayout();
setCompositionRoot(mainLayout);
// TODO add user code here
absoluteLayout_2.addComponent(buildToolbar(),"left: 0px; right: 0px; "+
"top: 0px; bottom: 0px;");
init();
}
private Component buildToolbar() {
HorizontalLayout header = new HorizontalLayout();
header.addStyleName("viewheader");
header.setSpacing(true);
Responsive.makeResponsive(header);
Label title = new Label("Transactions");
title.setSizeUndefined();
title.addStyleName(ValoTheme.LABEL_H1);
title.addStyleName(ValoTheme.LABEL_NO_MARGIN);
header.addComponent(title);
box = new ComboBox("Filter Property");
box.addItem("PAN");
box.addItem("Expiry Date");
box.addItem("Station Id");
box.addItem("Terminal Id");
box.addItem("Stan");
box.addItem("Authorized Amount");
box.addItem("Sale Amount");
box.addItem("Acquirer Id");
box.addItem("Authorize Code");
box.addItem("IS Settled");
box.addItem("IS Reversed");
box.addItem("Node Id");
field = new TextField("Value");
applyFilter = new Button("Apply Filter");
applyFilter.setIcon(FontAwesome.FILTER);
applyFilter.setDescription("Filter");
applyFilter.addClickListener(this);
remFilter = new Button("Remove Filter");
remFilter.setIcon(FontAwesome.RENREN);
remFilter.setDescription("Remove");
remFilter.addClickListener(this);
HorizontalLayout tools = new HorizontalLayout(box,field,applyFilter,remFilter);
tools.setSpacing(true);
tools.setWidthUndefined();
tools.addStyleName("toolbar");
tools.setComponentAlignment(box, Alignment.BOTTOM_RIGHT);
tools.setComponentAlignment(field, Alignment.BOTTOM_RIGHT);
tools.setComponentAlignment(applyFilter, Alignment.BOTTOM_RIGHT);
tools.setComponentAlignment(remFilter, Alignment.BOTTOM_RIGHT);
header.addComponent(tools);
header.setComponentAlignment(tools, Alignment.MIDDLE_RIGHT);
return header;
}
public void buttonClick(final ClickEvent event) {
if (event.getButton() == applyFilter) {
String nameFilter = field.getValue();
if (nameFilter != null && nameFilter.length() != 0 && box.getValue() != null) {
nameFilter = nameFilter.trim();
entityContainer.removeAllContainerFilters();
switch(box.getValue().toString()){
case "PAN":
try{
long la = Long.parseLong(nameFilter);
entityContainer.addContainerFilter(new Compare.Equal("pan",la));
}catch(Exception e){
Notification.show("PAN no should be numeric",Type.ERROR_MESSAGE);
}
break;
case "Expiry Date":
entityContainer.addContainerFilter(new Compare.Equal("expiryDate", nameFilter));
break;
case "Station Id":
entityContainer.addContainerFilter(new Compare.Equal("id.statTerminal.station.id", nameFilter));
break;
case "Terminal Id":
entityContainer.addContainerFilter(new Compare.Equal("id.statTerminal.terminalId", nameFilter));
break;
case "Stan":
entityContainer.addContainerFilter(new Compare.Equal("stan", nameFilter));
break;
case "Authorized Amount":
entityContainer.addContainerFilter(new Compare.Equal("authAmount", nameFilter));
break;
case "Sale Amount":
entityContainer.addContainerFilter(new Compare.Equal("saleAmount", nameFilter));
break;
case "Acquirer Id":
entityContainer.addContainerFilter(new Compare.Equal("acquirerId", nameFilter));
break;
case "Authorize Code":
entityContainer.addContainerFilter(new Compare.Equal("authCode", nameFilter));
break;
case "IS Settled":
entityContainer.addContainerFilter(new Compare.Equal("isSettled", nameFilter));
break;
case "IS Reversed":
entityContainer.addContainerFilter(new Compare.Equal("isReveresd", nameFilter));
break;
case "Node Id":
entityContainer.addContainerFilter(new Compare.Equal("nodeId", nameFilter));
break;
}
} else {
entityContainer.removeAllContainerFilters();
}
entityContainer.refresh();
}
if (event.getButton() == remFilter) {
entityContainer.removeAllContainerFilters();
entityContainer.refresh();
}
}
public void init() {
try{
Object[] visibleCols = {"localTime","id.statTerminal.station.id","id.statTerminal.terminalId","nodeId","stan","pan", "expiryDate", "authAmount","saleAmount","acquirerId","authCode","isSettled","isReveresd"};
table_1.setColumnHeader("pan", "PAN");
table_1.setColumnHeader("expiryDate", "Expiry Date");
table_1.setColumnHeader("id.statTerminal.station.id", "Station Id");
table_1.setColumnHeader("id.statTerminal.terminalId", "Terminal Id");
table_1.setColumnHeader("stan", "Stan");
table_1.setColumnHeader("localTime", "Local Time");
table_1.setColumnHeader("authAmount", "Authorized Amount");
table_1.setColumnHeader("saleAmount", "Sale Amount");
table_1.setColumnHeader("acquirerId", "Acquirer Id");
table_1.setColumnHeader("authCode", "Authorize Code");
table_1.setColumnHeader("isSettled", "IS Settled");
table_1.setColumnHeader("isReveresd", "IS Reversed");
table_1.setColumnHeader("nodeId", "Node Id");
final EntityManager entityManager = entityManagerFactory.createEntityManager();
entityContainer = new LazyEntityContainer<PayAuthorize>(entityManager, PayAuthorize.class, 100, "id.trxId", true, true, true);
entityContainer.getQueryView().getQueryDefinition().setDefaultSortState(
new Object[]{"localTime"}, new boolean[]
{false});
entityContainer.getQueryView().getQueryDefinition().setMaxNestedPropertyDepth(4);
entityContainer.addContainerProperty(LazyQueryView.PROPERTY_ID_ITEM_STATUS, QueryItemStatus.class,
QueryItemStatus.None, true, false);
entityContainer.addContainerProperty("id.trxId", Long.class, 0L, true, true);
entityContainer.addContainerProperty("id.statTerminal.station.id", String.class, "", true, true);
entityContainer.addContainerProperty("id.statTerminal.terminalId", String.class, "", true, true);
entityContainer.addContainerProperty("pan", Long.class, "", false, true);
entityContainer.addContainerProperty("expiryDate", String.class, "", true, true);
entityContainer.addContainerProperty("splitId", String.class, "", true, true);
entityContainer.addContainerProperty("stan", String.class, "", true, true);
entityContainer.addContainerProperty("localTime", Date.class, "", true, true);
entityContainer.addContainerProperty("authAmount", String.class, "", true, true);
entityContainer.addContainerProperty("saleAmount", String.class, "", true, true);
entityContainer.addContainerProperty("acquirerId", String.class, "", true, true);
entityContainer.addContainerProperty("authCode", String.class, "", true, true);
entityContainer.addContainerProperty("isSettled", Integer.class, "", true, true);
entityContainer.addContainerProperty("isReveresd", Integer.class, "", true, true);
entityContainer.addContainerProperty("nodeId", Integer.class, "", true, true);
entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_QUERY_INDEX, Integer.class, 0, true, false);
entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_INDEX, Integer.class, 0, true, false);
entityContainer.addContainerProperty(LazyQueryView.DEBUG_PROPERTY_ID_BATCH_QUERY_TIME, Long.class, 0, true,false);
table_1.setPageLength(5);
table_1.setContainerDataSource(entityContainer);
table_1.setVisibleColumns(visibleCols);
table_1.setVisible(true);
table_1.refreshRowCache();
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void enter(ViewChangeEvent event) {
}
@AutoGenerated
private AbsoluteLayout buildMainLayout() {
// common part: create layout
mainLayout = new AbsoluteLayout();
mainLayout.setImmediate(false);
mainLayout.setWidth("100%");
mainLayout.setHeight("100%");
// top-level component properties
setWidth("100.0%");
setHeight("100.0%");
// verticalLayout_1
verticalLayout_1 = buildVerticalLayout_1();
mainLayout.addComponent(verticalLayout_1,
"top:0.0px;right:0.0px;bottom:0.0px;left:0.0px;");
return mainLayout;
}
@AutoGenerated
private VerticalLayout buildVerticalLayout_1() {
// common part: create layout
verticalLayout_1 = new VerticalLayout();
verticalLayout_1.setImmediate(false);
verticalLayout_1.setWidth("100.0%");
verticalLayout_1.setHeight("100.0%");
verticalLayout_1.setMargin(false);
// absoluteLayout_2
absoluteLayout_2 = new AbsoluteLayout();
absoluteLayout_2.setImmediate(false);
absoluteLayout_2.setWidth("100.0%");
absoluteLayout_2.setHeight("100px");
verticalLayout_1.addComponent(absoluteLayout_2);
// table_1
table_1 = new Table();
table_1.setImmediate(false);
table_1.setWidth("100.0%");
table_1.setHeight("100.0%");
verticalLayout_1.addComponent(table_1);
verticalLayout_1.setExpandRatio(table_1, 1.0f);
return verticalLayout_1;
}
public void refresh() {
System.out.println("TRASNCTION REFRESH");
if(entityContainer !=null){
entityContainer.refresh();
}
}
}
---- Spring xml —
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceUnitName" value="myPersistenceUnit" />
<property name="packagesToScan" value="com.duc.epi.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.auto_close_session">true</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">10</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckin">true</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">200</prop>
<prop key="hibernate.c3p0.max_idle_time">200</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.c3p0.unreturnedConnectionTimeout">30</prop>
<prop key="hibernate.c3p0.debugUnreturnedConnectionStackTraces">true</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
</props>
</property>
</bean>