Table reset

Hi Vaadin :glare:

First I would like to say that I whish you a big success with this ‘new’ project. I like it and I’m waiting for final 6.0 release. Book of Vaadin is another great idea. I will buy it as soon as final release comes out.

Back to my question, I use Table component with QueryCointainer, like this:

QueryContainer container = new QueryContainer("select * from my_table", conn);
table.setContainerDataSource(container);

Then I would like to change data so I would like to use different sql query. But if I do this:

container = new QueryContainer("select * from my_second_table", conn); table.setContainerDataSource(container); my table just don’t change.

If I do this:

[code]

table = new Table();
container = new QueryContainer(“select * from my_table”, conn);
table.setContainerDataSource(container);
[/code]everything works fine, but I guess that this is not the right approach. And I’m getting PermGen errors.

So, is this second approach the right one?
thnx for any ideas and help
Janez

I’m sorry I was not logged in when I wrote this post…

Hi, Janez,

I successfully changing the table’s data by just setting a new datasource in 5.4, however, Im using BeanItem and HBN containers. I you’re seeing PermGen errors, Im guessing you’re hitting the out of memory problems with the heap. QueryContainer, AFAIK, does not buffer the data and loads entire sql resultset into the memory, in comparison to HBN container, so if your query brings a lot of rows, you may get heap problems. For now, try increasing your max heap space and see if this helps or not.

Best,
Dmitri

I had some similar problems with changing the datasource on the fly in one of my pet projects. I can’t recall exactly what triggered the problem, but the result was the red error window. I just did a workaround back then, that got it all working, but I guess I’ll go back to solving out what was wrong as I’m gonna use the code as example and it has to be therefore clean. It used an IndexedContainer and the data set was really small (RSS feeds).

Hi!
I (somehow) use 5.3.1 version, maybe that’s the issue here. I’ve solved PermGen problems (for now) with increased Heap, thank you for idea. I will try with 5.4 version. Yet better I will try with Vaadin:)
Janez

Hi!
I have tried this with vaadin-pre and it works the same. So I have to change from QueryContainer to some BeanContainer?
Janez

Hi,

Not sure if that will help you, but I have encountered the same problem (java.lang.OutOfMemoryError: Java heap space) when I try to load many data into Table component, the container used all java’s memory.
I Tried to change many things to optimize memory management on my project without a good success. The only solution I have is to increase allocated memory for Java on server, but it’s not the best solution.
That’s why I decided to develop my own container to use lazy loading with hibernate in order to optimize memory usage and loading time. To do that I based my work on
Hbncontainer Project
and today i can display table without any memory problem.
HbnContainer article can be found
here
.

Thomas this is excellent idea, but I think that the problem is in Table. Have you tried to change tables containerDataSource (or your HbnContainer). Something like this:

container = new XContainer(); 
....
table.setContainerDataSource(container);
container1 = new XContainer();
...
table.setContainerDataSource(container1);

Table just don’t get changed. table.removeAliItems is not supported, so the only thing is to create new Table object.

I think that the same problem (or very similiar) is also in this post


http://vaadin.com/forum/-/message_boards/message/32519
.

Thomas I will try with your container.
Janez

Hi Janez,

If I understand the problem, you can’t refresh your table with a new container. It’s seems to be strange, because currently I set my LazyContainer on table with setContainerDataSource and load new Container with the same method (like your example), and I have no problem to refresh my table.
Not sure if that can help you, but did you try to set your containerDataSource at null before instantiate new container ?

//Reload table with new container
table.setContainerDataSource(null);
table.setContainerDataSource(new CustomContainer());

Hi!

Yes I have tried with setContainerDataSource(null), but there’s no effect.
My workaround for this is:

//Setup container
container = new BeanItemContainer<Object>(coll);
//Create new table
Table table = new Table();
//Set data source
table.setContainerDataSource(container);
//Call util method
resetTable(table);

and resetTable method is:

private void resetTable(Table table){
 //Setup table		
 table.setSelectable(true);
 //Add listener		
 table.addListener(this);
 //Clear wrapper - vertical layout
 tableWrapper.removeAllComponents();
 tableWrapper.addComponent(table);
 toolbar.setVisible(true);
}

This works nice, but I don’t like that kind of approach:(

Thnx

Hum I understand you, but that should works without a workaround. Did you check with another container ?
Currently I just try with HierarchicalContainer under v5.3.1 and refresh table works fine.


import java.util.Random;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.util.HierarchicalContainer;
import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.event.Action.Handler;
import com.itmill.toolkit.ui.Table;
import com.itmill.toolkit.ui.Window;

public class TableTesting extends Application implements Handler {
	private Window mainWindow;
	private Table table = new Table();
	private Action loadTable = new Action("Reload");
	private Action[] actions = new Action[]
 { loadTable };
	private Random random = new Random();
	private String[] columns = new String[]
 { "Column1", "Column2", "Column3", "Column4" };

	@Override
	public void init() {
		mainWindow = new Window("Table Reload");
		mainWindow.getLayout().setSizeFull();
		setMainWindow(mainWindow);
		table.setSizeFull();
		table.addActionHandler(this);
		loadTable(random.nextInt(10));
		mainWindow.addComponent(table);

	}

	/**
	 * @param itemNumber
	 */
	private void loadTable(int itemNumber) {
		HierarchicalContainer container = createContainer();
		for (int j = 0; j < itemNumber; j++) {
			Item rowItem = container.addItem(j);
			if (rowItem != null) {
				for (int i = 0; i < columns.length; i++) {
					rowItem.getItemProperty(columns[i]
).setValue("Value" + j);
				}
			}
		}
		table.setContainerDataSource(container);
	}

	/**
	 * @return
	 */
	private HierarchicalContainer createContainer() {
		final HierarchicalContainer c = new HierarchicalContainer();
		for (int i = 0; i < columns.length; i++) {
			c.addContainerProperty(columns[i]
, String.class, null);
		}
		return c;
	}

	@Override
	public Action[] getActions(Object target, Object sender) {
		return actions;
	}

	@Override
	public void handleAction(Action action, Object sender, Object target) {
		if (action == loadTable) {
			loadTable(random.nextInt(100));
		}

	}
}

I hope that can help you…