1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package com.vaadin.tests.containers.sqlcontainer;
import java.sql.SQLException;
import com.vaadin.data.util.sqlcontainer.SQLContainer;
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.server.LegacyApplication;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.VerticalLayout;
// author table in testdb (MySQL) is set out as follows
// +-------------+-------------+------+-----+---------+----------------+
// | Field | Type | Null | Key | Default | Extra |
// +-------------+-------------+------+-----+---------+----------------+
// | id | int(11) | NO | PRI | NULL | auto_increment |
// | last_name | varchar(40) | NO | | NULL | |
// | first_names | varchar(80) | NO | | NULL | |
// +-------------+-------------+------+-----+---------+----------------+
@SuppressWarnings("serial")
public class MassInsertMemoryLeakTestApp extends LegacyApplication {
ProgressIndicator proggress = new ProgressIndicator();
Button process = new Button("Mass insert");
@Override
public void init() {
setMainWindow(new LegacyWindow("SQLContainer Test", buildLayout()));
process.addListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
MassInsert mi = new MassInsert();
mi.start();
}
});
}
private class MassInsert extends Thread {
@Override
public void start() {
getContext().lock();
try {
proggress.setVisible(true);
proggress.setValue(new Float(0));
proggress.setPollingInterval(100);
process.setEnabled(false);
proggress.setCaption("");
super.start();
} finally {
getContext().unlock();
}
}
@Override
public void run() {
JDBCConnectionPool pool = getConnectionPool();
if (pool != null) {
try {
int cents = 100;
for (int cent = 0; cent < cents; cent++) {
TableQuery q = new TableQuery("AUTHOR", pool);
q.setVersionColumn("ID");
SQLContainer c = new SQLContainer(q);
for (int i = 0; i < 100; i++) {
Object id = c.addItem();
c.getContainerProperty(id, "FIRST_NAMES").setValue(
getRandonName());
c.getContainerProperty(id, "LAST_NAME").setValue(
getRandonName());
}
c.commit();
getContext().lock();
try {
proggress
.setValue(new Float((1.0f * cent) / cents));
proggress.setCaption("" + 100 * cent
+ " rows inserted");
} finally {
getContext().unlock();
}
}
} catch (SQLException e) {
getMainWindow().showNotification(
"SQLException while processing",
e.getLocalizedMessage());
e.printStackTrace();
}
}
getContext().lock();
try {
proggress.setVisible(false);
proggress.setPollingInterval(0);
process.setEnabled(true);
} finally {
getContext().unlock();
}
}
}
private ComponentContainer buildLayout() {
VerticalLayout lo = new VerticalLayout();
lo.setSizeFull();
lo.addComponent(proggress);
lo.addComponent(process);
lo.setComponentAlignment(proggress, Alignment.BOTTOM_CENTER);
lo.setComponentAlignment(process, Alignment.TOP_CENTER);
lo.setSpacing(true);
proggress.setIndeterminate(false);
proggress.setVisible(false);
return lo;
}
private String getRandonName() {
final String[] tokens = new String[] { "sa", "len", "da", "vid", "ma",
"ry", "an", "na", "jo", "bri", "son", "mat", "e", "ric", "ge",
"eu", "han", "har", "ri", "ja", "lo" };
StringBuffer sb = new StringBuffer();
int len = (int) (Math.random() * 3 + 2);
while (len-- > 0) {
sb.append(tokens[(int) (Math.random() * tokens.length)]);
}
return Character.toUpperCase(sb.charAt(0)) + sb.toString().substring(1);
}
private JDBCConnectionPool getConnectionPool() {
SimpleJDBCConnectionPool pool = null;
try {
pool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/sqlcontainer", "sqlcontainer",
"sqlcontainer");
} catch (SQLException e) {
getMainWindow().showNotification("Error connecting to database");
}
return pool;
}
}
|