--- dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/09/11 08:14:40 2200 +++ dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/09/15 10:37:25 2220 @@ -14,9 +14,11 @@ import java.util.logging.Logger; + public class Database { Logger logger = Logger.getLogger(Database.class.getName()); + int batchCount = 0; Connection conn; PreparedStatement saveStmt; @@ -94,15 +96,20 @@ public List hentPostnumre() throws SQLException { ArrayList list = new ArrayList(); + + Constants consts = Constants.getInstance(); String sql = "SELECT postnr " + "FROM fulddaekning.adressetabel " + - "WHERE distributor = ? and rute is not null " + + //"WHERE distributor = ? and rute is not null " + + "WHERE postnr BETWEEN ? AND ? " + "GROUP BY postnr " + "ORDER by postnr"; PreparedStatement stmt = conn.prepareStatement(sql); - stmt.setString(1, Lookup.distributor ); + //stmt.setString(1, Lookup.distributor ); + stmt.setInt(1, consts.getMinPostnr()); + stmt.setInt(2, consts.getMaxPostnr()); ResultSet res = stmt.executeQuery(); while (res.next()) { @@ -117,7 +124,7 @@ return list; } - public ArrayList hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { + public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + "FROM fulddaekning.adressetabel " + "WHERE rute IS NOT NULL " + @@ -137,7 +144,8 @@ stmt.setDouble(4, bbox.longitudeMax); stmt.setString(5, Lookup.distributor); - return hentAdresseListe( stmt ); + List list = hentAdresseListe( stmt ); + return list.toArray( new Adresse[ list.size() ] ); } @@ -171,18 +179,27 @@ saveStmt.setDouble(19, bedsteAfstand); - saveStmt.executeUpdate(); - saveStmt.clearParameters(); - + saveStmt.addBatch(); + batchCount++; + if (batchCount >= 100) { + saveStmt.executeBatch(); + batchCount = 0; + } + //saveStmt.executeUpdate(); + //saveStmt.clearParameters(); //saveStmt.close(); - + } + + public synchronized void saveBatch() throws SQLException{ + saveStmt.executeBatch(); + batchCount = 0; } protected ArrayList hentAdresseListe(PreparedStatement stmt) throws SQLException{ - ArrayList list = new ArrayList( 30000 ); + ArrayList list = new ArrayList( 1000000 ); //logger.info("Starting query"); ResultSet res = stmt.executeQuery(); @@ -225,10 +242,11 @@ connectionProps.put("user", db_user); connectionProps.put("password", db_pass); + //For debug output, tilføj denne til JDBC url'en: &profileSQL=true conn = DriverManager.getConnection( "jdbc:mysql://" + db_host + - ":3306/", + ":3306/?rewriteBatchedStatements=true", connectionProps); logger.info("Connected to database"); return conn;