OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sql/connection.h" | 5 #include "sql/connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 open_statements_.clear(); | 249 open_statements_.clear(); |
250 | 250 |
251 if (db_) { | 251 if (db_) { |
252 // Call to AssertIOAllowed() cannot go at the beginning of the function | 252 // Call to AssertIOAllowed() cannot go at the beginning of the function |
253 // because Close() must be called from destructor to clean | 253 // because Close() must be called from destructor to clean |
254 // statement_cache_, it won't cause any disk access and it most probably | 254 // statement_cache_, it won't cause any disk access and it most probably |
255 // will happen on thread not allowing disk access. | 255 // will happen on thread not allowing disk access. |
256 // TODO(paivanof@gmail.com): This should move to the beginning | 256 // TODO(paivanof@gmail.com): This should move to the beginning |
257 // of the function. http://crbug.com/136655. | 257 // of the function. http://crbug.com/136655. |
258 AssertIOAllowed(); | 258 AssertIOAllowed(); |
259 // TODO(shess): Histogram for failure. | 259 |
260 sqlite3_close(db_); | 260 int rc = sqlite3_close(db_); |
| 261 if (rc != SQLITE_OK) { |
| 262 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc); |
| 263 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage(); |
| 264 } |
261 } | 265 } |
262 db_ = NULL; | 266 db_ = NULL; |
263 } | 267 } |
264 | 268 |
265 void Connection::Close() { | 269 void Connection::Close() { |
266 // If the database was already closed by RazeAndClose(), then no | 270 // If the database was already closed by RazeAndClose(), then no |
267 // need to close again. Clear the |poisoned_| bit so that incorrect | 271 // need to close again. Clear the |poisoned_| bit so that incorrect |
268 // API calls are caught. | 272 // API calls are caught. |
269 if (poisoned_) { | 273 if (poisoned_) { |
270 poisoned_ = false; | 274 poisoned_ = false; |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 // RazeAndClose(). Until regular Close() is called, the caller | 807 // RazeAndClose(). Until regular Close() is called, the caller |
804 // should be treating the database as open, but is_open() currently | 808 // should be treating the database as open, but is_open() currently |
805 // only considers the sqlite3 handle's state. | 809 // only considers the sqlite3 handle's state. |
806 // TODO(shess): Revise is_open() to consider poisoned_, and review | 810 // TODO(shess): Revise is_open() to consider poisoned_, and review |
807 // to see if any non-testing code even depends on it. | 811 // to see if any non-testing code even depends on it. |
808 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open."; | 812 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open."; |
809 poisoned_ = false; | 813 poisoned_ = false; |
810 | 814 |
811 int err = sqlite3_open(file_name.c_str(), &db_); | 815 int err = sqlite3_open(file_name.c_str(), &db_); |
812 if (err != SQLITE_OK) { | 816 if (err != SQLITE_OK) { |
| 817 // Extended error codes cannot be enabled until a handle is |
| 818 // available, fetch manually. |
| 819 err = sqlite3_extended_errcode(db_); |
| 820 |
813 // Histogram failures specific to initial open for debugging | 821 // Histogram failures specific to initial open for debugging |
814 // purposes. | 822 // purposes. |
815 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenFailure", err & 0xff, 50); | 823 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err); |
816 | 824 |
817 OnSqliteError(err, NULL); | 825 OnSqliteError(err, NULL); |
818 bool was_poisoned = poisoned_; | 826 bool was_poisoned = poisoned_; |
819 Close(); | 827 Close(); |
820 | 828 |
821 if (was_poisoned && retry_flag == RETRY_ON_POISON) | 829 if (was_poisoned && retry_flag == RETRY_ON_POISON) |
822 return OpenInternal(file_name, NO_RETRY); | 830 return OpenInternal(file_name, NO_RETRY); |
823 return false; | 831 return false; |
824 } | 832 } |
825 | 833 |
(...skipping 21 matching lines...) Expand all Loading... |
847 } | 855 } |
848 #endif // defined(OS_POSIX) | 856 #endif // defined(OS_POSIX) |
849 | 857 |
850 // SQLite uses a lookaside buffer to improve performance of small mallocs. | 858 // SQLite uses a lookaside buffer to improve performance of small mallocs. |
851 // Chromium already depends on small mallocs being efficient, so we disable | 859 // Chromium already depends on small mallocs being efficient, so we disable |
852 // this to avoid the extra memory overhead. | 860 // this to avoid the extra memory overhead. |
853 // This must be called immediatly after opening the database before any SQL | 861 // This must be called immediatly after opening the database before any SQL |
854 // statements are run. | 862 // statements are run. |
855 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0); | 863 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0); |
856 | 864 |
| 865 // Enable extended result codes to provide more color on I/O errors. |
| 866 // Not having extended result codes is not a fatal problem, as |
| 867 // Chromium code does not attempt to handle I/O errors anyhow. The |
| 868 // current implementation always returns SQLITE_OK, the DCHECK is to |
| 869 // quickly notify someone if SQLite changes. |
| 870 err = sqlite3_extended_result_codes(db_, 1); |
| 871 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes"; |
| 872 |
857 // sqlite3_open() does not actually read the database file (unless a | 873 // sqlite3_open() does not actually read the database file (unless a |
858 // hot journal is found). Successfully executing this pragma on an | 874 // hot journal is found). Successfully executing this pragma on an |
859 // existing database requires a valid header on page 1. | 875 // existing database requires a valid header on page 1. |
860 // TODO(shess): For now, just probing to see what the lay of the | 876 // TODO(shess): For now, just probing to see what the lay of the |
861 // land is. If it's mostly SQLITE_NOTADB, then the database should | 877 // land is. If it's mostly SQLITE_NOTADB, then the database should |
862 // be razed. | 878 // be razed. |
863 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum"); | 879 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum"); |
864 if (err != SQLITE_OK) | 880 if (err != SQLITE_OK) |
865 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenProbeFailure", err & 0xff, 50); | 881 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err); |
866 | |
867 // Enable extended result codes to provide more color on I/O errors. | |
868 // Not having extended result codes is not a fatal problem, as | |
869 // Chromium code does not attempt to handle I/O errors anyhow. The | |
870 // current implementation always returns SQLITE_OK, the DCHECK is to | |
871 // quickly notify someone if SQLite changes. | |
872 err = sqlite3_extended_result_codes(db_, 1); | |
873 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes"; | |
874 | 882 |
875 #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) | 883 #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) |
876 // The version of SQLite shipped with iOS doesn't enable ICU, which includes | 884 // The version of SQLite shipped with iOS doesn't enable ICU, which includes |
877 // REGEXP support. Add it in dynamically. | 885 // REGEXP support. Add it in dynamically. |
878 err = sqlite3IcuInit(db_); | 886 err = sqlite3IcuInit(db_); |
879 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support"; | 887 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support"; |
880 #endif // OS_IOS && USE_SYSTEM_SQLITE | 888 #endif // OS_IOS && USE_SYSTEM_SQLITE |
881 | 889 |
882 // If indicated, lock up the database before doing anything else, so | 890 // If indicated, lock up the database before doing anything else, so |
883 // that the following code doesn't have to deal with locking. | 891 // that the following code doesn't have to deal with locking. |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 } | 1032 } |
1025 | 1033 |
1026 // Best effort to put things back as they were before. | 1034 // Best effort to put things back as they were before. |
1027 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF"; | 1035 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF"; |
1028 ignore_result(Execute(kNoWritableSchema)); | 1036 ignore_result(Execute(kNoWritableSchema)); |
1029 | 1037 |
1030 return ret; | 1038 return ret; |
1031 } | 1039 } |
1032 | 1040 |
1033 } // namespace sql | 1041 } // namespace sql |
OLD | NEW |