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

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

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

revision 2210 by torben, Thu Sep 11 18:04:29 2014 UTC revision 2235 by torben, Tue Nov 11 13:45:21 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 78  public class Database { Line 85  public class Database {
85          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {
86                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
87    
88                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
89                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
90                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
91                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
92                                  "AND postnr=?  " +                                  "AND a.postnr=?  " +
93                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
94                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
95                                  "AND gadeid IS NOT NULL ";                                  "AND gadeid IS NOT NULL " +
96                                    "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
97                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
98                  stmt.setInt(1, postnr);                  stmt.setInt(1, postnr);
99    
# Line 123  public class Database { Line 132  public class Database {
132          }          }
133    
134          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
135                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
136                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
137                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
138                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
139                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
140                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
141                                  "AND distributor = ? ";                                  "AND a.distributor = ? ";
142    
143                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
144                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
# Line 144  public class Database { Line 154  public class Database {
154    
155                  List<Adresse> list = hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
156                  return list.toArray( new Adresse[ list.size() ] );                  return list.toArray( new Adresse[ list.size() ] );
   
157          }          }
158            
159            
160            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
161                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
162                                    "FROM fulddaekning.adressetabel a " +
163                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
164                                    "WHERE rute IS NOT NULL " +
165                                    "AND latitude IS NOT NULL " +
166                                    "AND longitude IS NOT NULL " +
167                                    "AND a.distributor = ? ";
168    
169                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
170                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
171                    //PreparedStatement stmt = conn.prepareStatement(sql);
172                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
173                    stmt.setFetchSize(Integer.MIN_VALUE);
174    
175                    stmt.setString(1, Lookup.distributor);
176    
177                    List<Adresse> list = hentAdresseListe( stmt );
178                    return list.toArray( new Adresse[ list.size() ] );
179            }
180            
181            
182    
183          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
184                  /*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 177  public class Database { Line 209  public class Database {
209    
210                  saveStmt.setDouble(19, bedsteAfstand);                  saveStmt.setDouble(19, bedsteAfstand);
211    
212                  saveStmt.executeUpdate();                        saveStmt.addBatch();
213                  saveStmt.clearParameters();                  batchCount++;
214                    if (batchCount >= 100) {
215                            saveStmt.executeBatch();
216                            batchCount = 0;
217                    }
218                    //saveStmt.executeUpdate();    
219                    //saveStmt.clearParameters();
220    
221                  //saveStmt.close();                      //saveStmt.close();    
222            }
223            
224            public synchronized void saveBatch() throws SQLException{
225                    saveStmt.executeBatch();
226                    batchCount = 0;
227          }          }
228    
229    
# Line 205  public class Database { Line 246  public class Database {
246                          adr.latitude = res.getDouble("latitude");                          adr.latitude = res.getDouble("latitude");
247                          adr.longitude = res.getDouble("longitude");                          adr.longitude = res.getDouble("longitude");
248                          adr.rute = res.getString("rute");                          adr.rute = res.getString("rute");
249                            adr.ho = res.getInt("ho");
250    
251                          list.add(adr);                          list.add(adr);
252    
# Line 231  public class Database { Line 273  public class Database {
273              connectionProps.put("user", db_user);              connectionProps.put("user", db_user);
274              connectionProps.put("password", db_pass);              connectionProps.put("password", db_pass);
275    
276                //For debug output, tilføj denne til JDBC url'en: &profileSQL=true    
277              conn = DriverManager.getConnection(              conn = DriverManager.getConnection(
278                             "jdbc:mysql://" +                             "jdbc:mysql://" +
279                             db_host +                             db_host +
280                             ":3306/",                             ":3306/?rewriteBatchedStatements=true",
281                             connectionProps);                             connectionProps);
282              logger.info("Connected to database");              logger.info("Connected to database");
283              return conn;              return conn;

Legend:
Removed from v.2210  
changed lines
  Added in v.2235

  ViewVC Help
Powered by ViewVC 1.1.20