我正在做一个连接到documentum数据存储的JavaFx项目。最近我注意到在某些特定点上它的表现非常缓慢。一旦它开始运行,它会从documentum中检索一些数据,考虑到数据量这个数据相当快,并创建我已定义的类的实例,以便将它们加载到表中。(直到这一点仍然可以。这是我的功能:
public void createCheckbox() {
List<String> columnnames = Arrays.asList("id", "name", "bg", "country","countryCode", "status",
"incorporationDate","aclName","aclDomain");
for (int i = 0; i <= columnnames.size() - 1 ; i++) {
ColumnConstraints column1 = new ColumnConstraints();
column1.setPercentWidth(22.0);
gridChkBoxContainer.getColumnConstraints().add(column1);
CheckBox cbs = new CheckBox(columnnames.get(i));
cbs.selectedProperty().set(true);
gridChkBoxContainer.add(cbs, i, 0);
}
}
public void createTable() {
int count = 0 ;
infoTable.getColumns().clear();
CheckBox cb;
ObservableList<Node> childrens = gridChkBoxContainer.getChildren();
for (Node node : childrens) {
cb = (CheckBox) node;
if (cb.isSelected()) {
TableColumn Col = new TableColumn(cb.getText());
Col.prefWidthProperty().bind(infoTable.widthProperty().divide(6));
Col.setCellValueFactory(new PropertyValueFactory<Report, String>(cb.getText()));
infoTable.getColumns().add(Col);
}
}
try{
infoTable.getItems().clear();
if (txtKeyword.getText().compareTo("") == 0){
strDQL = "select dm_dbo.cosec_general_view.name , dm_dbo.cosec_general_view.comp_id , dm_dbo.cosec_general_view.bg_name , dm_dbo.cosec_general_view.incorporation_date , dm_dbo.cosec_general_view.status , dm_dbo.cosec_general_view.country_name , cosec_general.acl_domain , cosec_general.acl_name , cosec_general.country_code from dm_dbo.cosec_general_view , cosec_general where dm_dbo.cosec_general_view.comp_id =cosec_general.comp_id";
}else{
strDQL = "select dm_dbo.cosec_general_view.name , dm_dbo.cosec_general_view.comp_id , dm_dbo.cosec_general_view.bg_name , dm_dbo.cosec_general_view.incorporation_date , dm_dbo.cosec_general_view.status , dm_dbo.cosec_general_view.country_name , cosec_general.acl_domain , cosec_general.acl_name , cosec_general.country_code from dm_dbo.cosec_general_view , cosec_general where dm_dbo.cosec_general_view.comp_id =cosec_general.comp_id and ( UPPER(dm_dbo.cosec_general_view.comp_id) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.name) like UPPER('@aclname')or UPPER(dm_dbo.cosec_general_view.bg_name) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.incorporation_date) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.country_name) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.status) like UPPER('@aclname') or UPPER(cosec_general.acl_name) like UPPER('@aclname')or UPPER(cosec_general.acl_domain) like UPPER('@aclname')or UPPER(cosec_general.country_code) like UPPER('@aclname') )";
String searchVar = "%"+txtKeyword.getText()+"%";
strDQL = strDQL.replace("@aclname", searchVar );
}
IDfQuery ACLQuery = new DfQuery();
ACLQuery.setDQL(strDQL);
IDfCollection col = ACLQuery.execute(_session, DfQuery.DF_READ_QUERY);
while (col.next()) {
BGDetails bgInfo = new BGDetails(col.getString("comp_id"), col.getString("name"), col.getString("bg_name"),
col.getString("country_name"), col.getString("status"), col.getString("incorporation_date"),col.getString("acl_name"),col.getString("acl_domain"),col.getString("country_code"));
infoTable.getItems().add(bgInfo);
count++;
}}catch(Exception e){
Alert alert = new Alert (Alert.AlertType.ERROR , "An Error was found!");
alert.showAndWait();
}
if (count == 0 ){
Alert alert = new Alert(Alert.AlertType.WARNING , "No search result was found !");
alert.showAndWait();
}
}
然后在顶部有文本字段(fx:id =&#34; txtKeyword&#34;),用于搜索数据。值得一提的是,为了这个目的而不是存储我之前提取的数据,我将创建并自定义新的DQL以再次从数据存储中重新获取。我已经注意到,此时不是数据检索需要很长时间才能运行,而是将单词键入到我的搜索字段中会明显滞后。换句话说,每当我想使用键盘或鼠标浏览应用程序时,它就会开始滞后,其他时候都可以。任何想法如何提高性能的速度? 在此先感谢:)
答案 0 :(得分:2)
与JavaFX Thread
无关的任何内容都可以在外部Thread
上完成,尤其是数据库查询。
JavaFX服务的示例结构[tutorial](以下是使用SQLITE的示例):
public class DataLoader extends Service<Boolean> {
int total;
/**
* Constructor
*/
public DataLoader() {
// if succeeded
setOnSucceeded(s -> {
//code if Service succeeds
});
// if failed
setOnFailed(fail -> {
//code it Service fails
});
//if canceled
setOnCancelled(cancelled->{
//code if Service get's cancelled
});
}
@Override
protected Task<Boolean> createTask() {
return new Task<Boolean>() {
@Override
protected Void call() throws Exception {
// -------------------------- Load all the libraries
try (ResultSet resultSet = connection1.createStatement().executeQuery("SELECT* FROM LIBRARIES;");
ResultSet dbCounter = connection1.createStatement()
.executeQuery("SELECT COUNT(*) FROM LIBRARIES;");) {
total = dbCounter.getInt(1);
Main.logger.info("Uploading libraries....");
// Refresh the text
Platform.runLater(() -> Main.updateScreen.label.setText("Uploading Libraries..."));
updateProgress(1, 2);
// Load all the libraries
while (resultSet.next()) {
Library library = new Library(resultSet.getString("NAME"), resultSet.getString("TABLENAME"),
resultSet.getDouble("STARS"), resultSet.getString("DATECREATED"),
resultSet.getString("TIMECREATED"), resultSet.getString("DESCRIPTION"),
resultSet.getInt("SAVEMODE"), resultSet.getInt("POSITION"),
resultSet.getString("LIBRARYIMAGE"), resultSet.getBoolean("OPENED"));
//Using Platform Later to Modify JavaFX Nodes
Platform.runLater(()->{
//code here
});
updateProgress(resultSet.getRow(), total);
}
} catch (Exception ex) {
Main.logger.log(Level.SEVERE, "", ex);
return false //something bad happened
}
return true;
}
};
}
}