Sqlite中的奇怪错误

时间:2014-02-13 08:01:00

标签: android transactions android-sqlite database-locking

在我的问题情况下,我使用服务每天在特定时间更新数据库,以提高插入速度我已将数据库插入操作放入try-catch-finally块并使用了事务,这是我最终成功并在那里结束。这些都适用于某些手机上的瑕疵,但在双核和4.0软糖的三星Duos上崩溃。我得到的错误信息是:

02-15 00:00:28.435: W/SQLiteConnectionPool(11250): The connection pool for database '+data+data+com_exa_xxx+databases+Bdr' has been unable to grant a connection to thread 1 (main) with flags 0x5 for 28.005001 seconds.
02-15 00:00:28.435: W/SQLiteConnectionPool(11250): Connections: 0 active, 1 idle, 0 available.

因此我的应用程序不会崩溃,但服务需要永远完成。其他手机的情况并非如此。

以下是我的插入函数的完整代码:

public void refreshRecords(){
    dbHelper.beginTransaction();



    try{
        System.out.println("in refresh");
        int years; 
        long secon; 
        long min; 
        int hours;
        int mon; 
        int days;
        int weeks;
        String CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI; 
        String dateToInsert;

        intCal = new IntervalCalculation();


        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, h:mm a");
        String datex = df.format(Calendar.getInstance().getTime());
        SharedPreferences prefsx = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Editor editorx = prefsx.edit();
        editorx.putString("refresh", datex);
        editorx.commit(); 

        Cursor c = dbHelper.fetchAdhoc(); 
        dbHelper.NotificationDrop(); 


        if (c.moveToFirst()) {
            do {
                try{
                    String name = c.getString(c.getColumnIndexOrThrow("Name"));
                    String displayBirthday = c.getString(c.getColumnIndexOrThrow("DOB"));
                    String imageData = c.getString(c.getColumnIndexOrThrow("ImageData"));
                    //CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI

                    CONTACT_ID = "10203040506070";
                    CONTACT_NAME = toTitleCase(name); 
                    CONTACT_IMAGE_URI = imageData; 
                    dateToInsert = displayBirthday; 

                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = format.parse(dateToInsert);
                    java.sql.Date dx = new java.sql.Date(date.getTime());
                    Date key = dx;

                    years = getDiffYear(key);               // For years elapsed
                    secon = seconds(key);                   // for seconds elapsed
                    min = seconds(key) / 60;                // For minutes elapsed
                    hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
                    mon = months(String.valueOf(key));      // for months elapsed

                    days = daysElapsed(key);                // Days elapsed
                    weeks = daysElapsed(key) / 7;           // For weeks

                    dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));

                    int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table. 
                    String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row. 



                    //=========================================================================
                    //**Database Insertions Notifications Table/ SpecialNotifications Table**
                    //=========================================================================



                    //=======================================================================================//
                    //Regular intervals DB Insertions: 
                    //======================================================================================//
                    //Notification Types:
                    //1 for months
                    //2 for weeks
                    //3 for days
                    //4 for minutes
                    //5 for years
                    //6 for seconds
                    //7 for hours
                    //======================================================================================//

                    //==============================
                    //For Months 
                    //==============================
                    intCal.monthsNotify(mon, dateToInsert);
                    int monSpecial =  intCal.getMonthRegular(); 
                    Date dateMonReg = intCal.getMonRegDate(); 


                    dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //===============================
                    //For Weeks 
                    //===============================
                    intCal.weeksToNotify(weeks,dateToInsert); 
                    int weekSpecial = intCal.getWeekRegular();
                    Date dateWeekReg =intCal.getWeekRegDate(); 

                    dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);
                    //===============================
                    //For Days                      
                    //===============================
                    intCal.daysToNotify(days, dateToInsert); 
                    int daysSpecial= intCal.getDaysRegular();  
                    Date dateDaysReg = intCal.getDaysRegDate(); 
                    dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);
                    //===============================
                    //For minutes
                    //===============================
                    intCal.minutesToNotify(min,dateToInsert);
                    long minutesSpecial= intCal.getMinutesRegular();
                    Date dateMinsReg = intCal.getMinutesRegDate(); 
                    dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //==============================
                    //For Years
                    //==============================
                    intCal.yearsToNotify(years, dateToInsert); 
                    int yearsSpecial = intCal.getYearsRegular();
                    Date dateYearsReg = intCal.getYearsRegDate();
                    dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //=============================
                    //For Seconds
                    //=============================
                    intCal.secondsToNotify(secon, dateToInsert);
                    long secondsSpecial= intCal.getSecondsRegular(); 
                    Date dateSecondsReg = intCal.getSecondsRegDate(); 
                    dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //=============================
                    //For Hours
                    //=============================
                    intCal.hoursToNotify(hours, dateToInsert); 
                    int hoursSpecial= intCal.getHoursRegular();
                    Date dateHoursReg= intCal.getHoursRegDate(); 
                    dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);



                    //============================================================================================//
                    //Special Intervals
                    //============================================================================================//
                    //Notification Types:
                    //1 for months
                    //2 for weeks
                    //3 for days
                    //4 for minutes
                    //5 for years
                    //6 for seconds
                    //7 for hours
                    //For Years
                    intCal.specialIntervalYears(years, dateToInsert); 
                    int yearsOnceSpecial =intCal.getYearsSpecial();
                    Date dateYearsSpecial = intCal.getYearsSpDate(); 
                    dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //For Months
                    intCal.specialIntervalMonths(mon,dateToInsert); 
                    int monthsOnceSpecial= intCal.getMonthsSpecial();
                    Date dateMonthsSpecial = intCal.getMonthsSpDate();
                    dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //For Weeks
                    intCal.specialIntervalsWeeks(weeks,dateToInsert); 
                    int weeksOnceSpecial= intCal.getWeeksSpecial(); 
                    Date dateWeeksSpecial = intCal.getWeeksSpDate(); 
                    dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Days
                    intCal.specialIntervalsDays(days, dateToInsert); 
                    int daysOnceSpecial= intCal.getDaysSpecial(); 
                    Date dateDaysSpecial = intCal.getDaysSpDate(); 
                    dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Hours
                    intCal.specialIntervalsHours(hours,dateToInsert); 
                    int hoursOnceSpecial= intCal.getHoursSpecial();  
                    Date dateHoursSpecial = intCal.getHoursSpDate(); 
                    dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Minutes
                    intCal.specialIntervalMinutes(min,dateToInsert); 
                    long minutesOnceSpecial= intCal.getMinutesSpecial(); 
                    Date dateMinutesSpecial= intCal.getMinutesSpDate(); 
                    dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Seconds
                    intCal.specialIntervalsSeconds(secon,dateToInsert); 
                    long secondsOnceSpecial= intCal.getSecondsSpecial(); 
                    Date dateSecondsSpecial= intCal.getSecondsSpDate(); 
                    dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID); 
                }catch(Exception e){

                }
            }while(c.moveToNext()); 
        }

        File toRead = null;
        try{
            File file = new File(getApplicationContext().getFilesDir()+"/BirthdayReminders/output.txt");
            if (file.exists()) {
                BufferedReader br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/BirthdayReminders/output.txt"));     
                if (br.readLine() != null) {
                    toRead=new File(getApplicationContext().getFilesDir()+"/BirthdayReminders/output.txt");
                }else{
                    System.out.println("F1");
                    toRead=new File(getApplicationContext().getFilesDir()+"/BirthdayReminders/fileone.txt");
                }
            }else{
                toRead=new File(getApplicationContext().getFilesDir()+"/BirthdayReminders/fileone.txt");
            }

            FileInputStream fis = new FileInputStream(toRead);
            Scanner sc=new Scanner(fis);
            String currentLine;
            int count = 0;

            while(sc.hasNextLine()){
                count++;
                currentLine=sc.nextLine();
                StringTokenizer st=new StringTokenizer(currentLine,"=",false);
                CONTACT_NAME = toTitleCase(st.nextToken());
                if(CONTACT_NAME.contains("'")){
                    CONTACT_NAME = CONTACT_NAME.replace("'", "");
                }
                System.out.println(CONTACT_NAME);
                // *********
                String listStr = st.nextToken();
                String cut = listStr.substring(1, listStr.length() - 1);
                String[] array = cut.split(",");
                CONTACT_ID = (array[0].trim());
                System.out.println(CONTACT_ID);
                String dateStr = (array[1].trim());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(dateStr);
                java.sql.Date dx = new java.sql.Date(date.getTime());
                Date key = dx;
                dateToInsert = String.valueOf(dx); 

                CONTACT_IMAGE_URI = (array[2].trim());
                System.out.println(CONTACT_IMAGE_URI);

                years = getDiffYear(key);               // For years elapsed
                System.out.println(years);

                secon = seconds(key);                   // for seconds elapsed
                System.out.println(secon);
                min = seconds(key) / 60;                // For minutes elapsed
                System.out.println(min);
                hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
                System.out.println(hours);
                mon = months(String.valueOf(key));      // for months elapsed
                System.out.println(mon);
                days = daysElapsed(key);                // Days elapsed
                System.out.println(days);
                weeks = daysElapsed(key) / 7;           // For weeks
                System.out.println(weeks);
                //dbHelper.open();
                //===============================================================================================================
                if (dateToInsert.contains("0001-") == true){ //Special Case, we added 0001 to Birthdays Which Have NO Year field. 
                    //===========================================================================================================

                    dbHelper.insert(dateToInsert, CONTACT_NAME, "","", CONTACT_IMAGE_URI, "", "", "", CONTACT_ID, "", ""); // All other fields will be empty, because we don't have a Year. 
                    int PRIMARY_ID = dbHelper.getPrimaryId();
                    String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); 


                    //=====================================================================================================
                    //In this case we are only interested in fetching the year alert for next birthday of this contact -->
                    //=====================================================================================================

                    intCal.yearsToNotify(years, dateToInsert); 
                    int yearsSpecial = intCal.getYearsRegular();
                    Date dateYearsReg = intCal.getYearsRegDate();

                    dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID); 

                }
                //=========================================================================
                //Case when all the Date fields exist and we set up notifications  --->
                //=========================================================================
                else if(dateToInsert != "null" && dateToInsert.contains("0001-") != true){

                    dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));

                    int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table. 
                    String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row. 



                    //=========================================================================
                    //**Database Insertions Notifications Table/ SpecialNotifications Table**
                    //=========================================================================



                    //=======================================================================================//
                    //Regular intervals DB Insertions: 
                    //======================================================================================//
                    //Notification Types:
                    //1 for months
                    //2 for weeks
                    //3 for days
                    //4 for minutes
                    //5 for years
                    //6 for seconds
                    //7 for hours
                    //======================================================================================//

                    //==============================
                    //For Months 
                    //==============================
                    intCal.monthsNotify(mon, dateToInsert);
                    int monSpecial =  intCal.getMonthRegular(); 
                    Date dateMonReg = intCal.getMonRegDate(); 


                    dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //===============================
                    //For Weeks 
                    //===============================
                    intCal.weeksToNotify(weeks,dateToInsert); 
                    int weekSpecial = intCal.getWeekRegular();
                    Date dateWeekReg =intCal.getWeekRegDate(); 

                    dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //===============================
                    //For Days
                    //===============================
                    intCal.daysToNotify(days, dateToInsert); 
                    int daysSpecial= intCal.getDaysRegular();  
                    Date dateDaysReg = intCal.getDaysRegDate(); 

                    dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //===============================
                    //For minutes
                    //===============================
                    intCal.minutesToNotify(min,dateToInsert);
                    long minutesSpecial= intCal.getMinutesRegular();
                    Date dateMinsReg = intCal.getMinutesRegDate(); 

                    dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //==============================
                    //For Years
                    //==============================
                    intCal.yearsToNotify(years, dateToInsert); 
                    int yearsSpecial = intCal.getYearsRegular();
                    Date dateYearsReg = intCal.getYearsRegDate();

                    dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //=============================
                    //For Seconds
                    //=============================
                    intCal.secondsToNotify(secon, dateToInsert);
                    long secondsSpecial= intCal.getSecondsRegular(); 
                    Date dateSecondsReg = intCal.getSecondsRegDate(); 

                    dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //=============================
                    //For Hours
                    //=============================
                    intCal.hoursToNotify(hours, dateToInsert); 
                    int hoursSpecial= intCal.getHoursRegular();
                    Date dateHoursReg= intCal.getHoursRegDate(); 

                    dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);



                    //============================================================================================//
                    //Special Intervals
                    //===========================================================================================//
                    //Notification Types:
                    //1 for months
                    //2 for weeks
                    //3 for days
                    //4 for minutes
                    //5 for years
                    //6 for seconds
                    //7 for hours
                    //For Years
                    intCal.specialIntervalYears(years, dateToInsert); 
                    int yearsOnceSpecial =intCal.getYearsSpecial();
                    Date dateYearsSpecial = intCal.getYearsSpDate(); 
                    dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //For Months
                    intCal.specialIntervalMonths(mon,dateToInsert); 
                    int monthsOnceSpecial= intCal.getMonthsSpecial();
                    Date dateMonthsSpecial = intCal.getMonthsSpDate();
                    dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //For Weeks
                    intCal.specialIntervalsWeeks(weeks,dateToInsert); 
                    int weeksOnceSpecial= intCal.getWeeksSpecial(); 
                    Date dateWeeksSpecial = intCal.getWeeksSpDate(); 
                    dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Days
                    intCal.specialIntervalsDays(days, dateToInsert); 
                    int daysOnceSpecial= intCal.getDaysSpecial(); 
                    Date dateDaysSpecial = intCal.getDaysSpDate(); 
                    dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Hours
                    intCal.specialIntervalsHours(hours,dateToInsert); 
                    int hoursOnceSpecial= intCal.getHoursSpecial();  
                    Date dateHoursSpecial = intCal.getHoursSpDate(); 
                    dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);

                    //For Minutes
                    intCal.specialIntervalMinutes(min,dateToInsert); 
                    long minutesOnceSpecial= intCal.getMinutesSpecial(); 
                    Date dateMinutesSpecial= intCal.getMinutesSpDate(); 
                    dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);


                    //For Seconds
                    intCal.specialIntervalsSeconds(secon,dateToInsert); 
                    long secondsOnceSpecial= intCal.getSecondsSpecial(); 
                    Date dateSecondsSpecial= intCal.getSecondsSpDate(); 
                    dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID); 
                    System.out.println("Iterating Count: "+count);
                }

            }
            System.out.println("Final Count: "+count);
            LC.writeToSD();
        }catch (Exception e){
            System.out.println("Inner Catch in service"+e);
        }
    }catch(Exception e){
        System.out.println("Catch in service"+e);
    }finally{
        dbHelper.setTransactionSuccessful();
        dbHelper.endTransaction();
        dbHelper.close();
    }

}

注意:我从异步任务调用上述方法,我在服务中启动。

1 个答案:

答案 0 :(得分:0)

检查您在另一段尚未完成的代码中是否有交易。这是导致此错误的最可能原因。

确保您没有多次启动AsyncTask,并且您在dbhelper中调用的方法正确地打开和关闭所有内部事务。