/[projects]/dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/Database.java
ViewVC logotype

Diff of /dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/Database.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2203 by torben, Thu Sep 11 14:46:59 2014 UTC revision 2230 by torben, Mon Sep 29 18:15:48 2014 UTC
# Line 14  import java.util.concurrent.ConcurrentLi Line 14  import java.util.concurrent.ConcurrentLi
14  import java.util.logging.Logger;  import java.util.logging.Logger;
15    
16    
17    
18  public class Database {  public class Database {
19          Logger logger = Logger.getLogger(Database.class.getName());          Logger logger = Logger.getLogger(Database.class.getName());
20    
21            int batchCount = 0;
22    
23          Connection conn;          Connection conn;
24          PreparedStatement saveStmt;          PreparedStatement saveStmt;
# Line 42  public class Database { Line 44  public class Database {
44          }                }      
45                    
46          public void renameResultTables() throws SQLException {          public void renameResultTables() throws SQLException {
47                    Constants consts = Constants.getInstance();
48                    String ext = consts.getTableExtension();
49                    
50                  logger.info("Dropping old backup table (if exists)");                  logger.info("Dropping old backup table (if exists)");
51                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old";                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext;
52                  conn.createStatement().executeUpdate(sql);                  conn.createStatement().executeUpdate(sql);
53                                    
54                  logger.info("Rename tables");                  logger.info("Rename tables");
55                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute";                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext;
56    
57                    logger.info("Executing: " + sql);
58                  conn.createStatement().executeUpdate(sql);                                conn.createStatement().executeUpdate(sql);              
59          }                }      
60    
# Line 122  public class Database { Line 129  public class Database {
129                  return list;                  return list;
130          }          }
131    
132          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
133                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
134                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel " +
135                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
# Line 142  public class Database { Line 149  public class Database {
149                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
150                  stmt.setString(5, Lookup.distributor);                  stmt.setString(5, Lookup.distributor);
151    
152                  return hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
153                    return list.toArray( new Adresse[ list.size() ] );
154          }          }
155            
156            
157            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
158                    String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
159                                    "FROM fulddaekning.adressetabel " +
160                                    "WHERE rute IS NOT NULL " +
161                                    "AND latitude IS NOT NULL " +
162                                    "AND longitude IS NOT NULL " +
163                                    "AND distributor = ? ";
164    
165                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
166                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
167                    //PreparedStatement stmt = conn.prepareStatement(sql);
168                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
169                    stmt.setFetchSize(Integer.MIN_VALUE);
170    
171                    stmt.setString(1, Lookup.distributor);
172    
173                    List<Adresse> list = hentAdresseListe( stmt );
174                    return list.toArray( new Adresse[ list.size() ] );
175            }
176            
177            
178    
179          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
180                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
# Line 176  public class Database { Line 205  public class Database {
205    
206                  saveStmt.setDouble(19, bedsteAfstand);                  saveStmt.setDouble(19, bedsteAfstand);
207    
208                  saveStmt.executeUpdate();                        saveStmt.addBatch();
209                  saveStmt.clearParameters();                  batchCount++;
210                    if (batchCount >= 100) {
211                            saveStmt.executeBatch();
212                            batchCount = 0;
213                    }
214                    //saveStmt.executeUpdate();    
215                    //saveStmt.clearParameters();
216    
217                  //saveStmt.close();                      //saveStmt.close();    
218            }
219            
220            public synchronized void saveBatch() throws SQLException{
221                    saveStmt.executeBatch();
222                    batchCount = 0;
223          }          }
224    
225    
226    
227          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
228                  ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 );                  ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
229    
230                  //logger.info("Starting query");                  //logger.info("Starting query");
231                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
# Line 230  public class Database { Line 268  public class Database {
268              connectionProps.put("user", db_user);              connectionProps.put("user", db_user);
269              connectionProps.put("password", db_pass);              connectionProps.put("password", db_pass);
270    
271                //For debug output, tilføj denne til JDBC url'en: &profileSQL=true    
272              conn = DriverManager.getConnection(              conn = DriverManager.getConnection(
273                             "jdbc:mysql://" +                             "jdbc:mysql://" +
274                             db_host +                             db_host +
275                             ":3306/",                             ":3306/?rewriteBatchedStatements=true",
276                             connectionProps);                             connectionProps);
277              logger.info("Connected to database");              logger.info("Connected to database");
278              return conn;              return conn;

Legend:
Removed from v.2203  
changed lines
  Added in v.2230

  ViewVC Help
Powered by ViewVC 1.1.20