| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.database.sqlite; |
| 18 |
|
| 19 |
import android.database.sqlite.SQLiteDebug.DbStats; |
| 20 |
import android.os.CancellationSignal; |
| 21 |
import android.os.Handler; |
| 22 |
import android.os.Looper; |
| 23 |
import android.os.Message; |
| 24 |
import android.os.OperationCanceledException; |
| 25 |
import android.os.SystemClock; |
| 26 |
import android.text.TextUtils; |
| 27 |
import android.util.ArraySet; |
| 28 |
import android.util.Log; |
| 29 |
import android.util.PrefixPrinter; |
| 30 |
import android.util.Printer; |
| 31 |
|
| 32 |
import com.android.internal.annotations.GuardedBy; |
| 33 |
import com.android.internal.annotations.VisibleForTesting; |
| 34 |
|
| 35 |
import dalvik.system.CloseGuard; |
| 36 |
|
| 37 |
import java.io.Closeable; |
| 38 |
import java.io.File; |
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Map; |
| 41 |
import java.util.WeakHashMap; |
| 42 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 43 |
import java.util.concurrent.atomic.AtomicLong; |
| 44 |
import java.util.concurrent.locks.LockSupport; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public final class SQLiteConnectionPool implements Closeable { |
| 83 |
private static final String TAG = "SQLiteConnectionPool"; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static final long CONNECTION_POOL_BUSY_MILLIS = 30 * 1000; |
| 88 |
|
| 89 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 90 |
|
| 91 |
private final Object mLock = new Object(); |
| 92 |
private final AtomicBoolean mConnectionLeaked = new AtomicBoolean(); |
| 93 |
private final SQLiteDatabaseConfiguration mConfiguration; |
| 94 |
private int mMaxConnectionPoolSize; |
| 95 |
private boolean mIsOpen; |
| 96 |
private int mNextConnectionId; |
| 97 |
|
| 98 |
private ConnectionWaiter mConnectionWaiterPool; |
| 99 |
private ConnectionWaiter mConnectionWaiterQueue; |
| 100 |
|
| 101 |
|
| 102 |
private final ArrayList<SQLiteConnection> mAvailableNonPrimaryConnections = |
| 103 |
new ArrayList<SQLiteConnection>(); |
| 104 |
private SQLiteConnection mAvailablePrimaryConnection; |
| 105 |
|
| 106 |
@GuardedBy("mLock") |
| 107 |
private IdleConnectionHandler mIdleConnectionHandler; |
| 108 |
|
| 109 |
|
| 110 |
private final AtomicLong mTotalStatementsTime = new AtomicLong(0); |
| 111 |
|
| 112 |
|
| 113 |
private final AtomicLong mTotalStatementsCount = new AtomicLong(0); |
| 114 |
|
| 115 |
|
| 116 |
enum AcquiredConnectionStatus { |
| 117 |
|
| 118 |
NORMAL, |
| 119 |
|
| 120 |
|
| 121 |
RECONFIGURE, |
| 122 |
|
| 123 |
|
| 124 |
DISCARD, |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private final WeakHashMap<SQLiteConnection, AcquiredConnectionStatus> mAcquiredConnections = |
| 133 |
new WeakHashMap<SQLiteConnection, AcquiredConnectionStatus>(); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static final int CONNECTION_FLAG_READ_ONLY = 1 << 0; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public static final int CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY = 1 << 1; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static final int CONNECTION_FLAG_INTERACTIVE = 1 << 2; |
| 166 |
|
| 167 |
private SQLiteConnectionPool(SQLiteDatabaseConfiguration configuration) { |
| 168 |
mConfiguration = new SQLiteDatabaseConfiguration(configuration); |
| 169 |
setMaxConnectionPoolSizeLocked(); |
| 170 |
|
| 171 |
|
| 172 |
if (mConfiguration.idleConnectionTimeoutMs != Long.MAX_VALUE) { |
| 173 |
setupIdleConnectionHandler(Looper.getMainLooper(), |
| 174 |
mConfiguration.idleConnectionTimeoutMs); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
protected void finalize() throws Throwable { |
| 180 |
try { |
| 181 |
dispose(true); |
| 182 |
} finally { |
| 183 |
super.finalize(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static SQLiteConnectionPool open(SQLiteDatabaseConfiguration configuration) { |
| 196 |
if (configuration == null) { |
| 197 |
throw new IllegalArgumentException("configuration must not be null."); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
SQLiteConnectionPool pool = new SQLiteConnectionPool(configuration); |
| 202 |
pool.open(); |
| 203 |
return pool; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
private void open() { |
| 208 |
|
| 209 |
|
| 210 |
mAvailablePrimaryConnection = openConnectionLocked(mConfiguration, |
| 211 |
true ); |
| 212 |
|
| 213 |
synchronized (mLock) { |
| 214 |
if (mIdleConnectionHandler != null) { |
| 215 |
mIdleConnectionHandler.connectionReleased(mAvailablePrimaryConnection); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
mIsOpen = true; |
| 221 |
mCloseGuard.open("SQLiteConnectionPool.close"); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void close() { |
| 236 |
dispose(false); |
| 237 |
} |
| 238 |
|
| 239 |
private void dispose(boolean finalized) { |
| 240 |
if (mCloseGuard != null) { |
| 241 |
if (finalized) { |
| 242 |
mCloseGuard.warnIfOpen(); |
| 243 |
} |
| 244 |
mCloseGuard.close(); |
| 245 |
} |
| 246 |
|
| 247 |
if (!finalized) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
synchronized (mLock) { |
| 253 |
throwIfClosedLocked(); |
| 254 |
|
| 255 |
mIsOpen = false; |
| 256 |
|
| 257 |
closeAvailableConnectionsAndLogExceptionsLocked(); |
| 258 |
|
| 259 |
final int pendingCount = mAcquiredConnections.size(); |
| 260 |
if (pendingCount != 0) { |
| 261 |
Log.i(TAG, "The connection pool for " + mConfiguration.label |
| 262 |
+ " has been closed but there are still " |
| 263 |
+ pendingCount + " connections in use. They will be closed " |
| 264 |
+ "as they are released back to the pool."); |
| 265 |
} |
| 266 |
|
| 267 |
wakeConnectionWaitersLocked(); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public void reconfigure(SQLiteDatabaseConfiguration configuration) { |
| 286 |
if (configuration == null) { |
| 287 |
throw new IllegalArgumentException("configuration must not be null."); |
| 288 |
} |
| 289 |
|
| 290 |
synchronized (mLock) { |
| 291 |
throwIfClosedLocked(); |
| 292 |
|
| 293 |
boolean isWalCurrentMode = mConfiguration.resolveJournalMode().equalsIgnoreCase( |
| 294 |
SQLiteDatabase.JOURNAL_MODE_WAL); |
| 295 |
boolean isWalNewMode = configuration.resolveJournalMode().equalsIgnoreCase( |
| 296 |
SQLiteDatabase.JOURNAL_MODE_WAL); |
| 297 |
boolean walModeChanged = isWalCurrentMode ^ isWalNewMode; |
| 298 |
if (walModeChanged) { |
| 299 |
|
| 300 |
|
| 301 |
if (!mAcquiredConnections.isEmpty()) { |
| 302 |
throw new IllegalStateException("Write Ahead Logging (WAL) mode cannot " |
| 303 |
+ "be enabled or disabled while there are transactions in " |
| 304 |
+ "progress. Finish all transactions and release all active " |
| 305 |
+ "database connections first."); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked(); |
| 311 |
assert mAvailableNonPrimaryConnections.isEmpty(); |
| 312 |
} |
| 313 |
|
| 314 |
boolean foreignKeyModeChanged = configuration.foreignKeyConstraintsEnabled |
| 315 |
!= mConfiguration.foreignKeyConstraintsEnabled; |
| 316 |
if (foreignKeyModeChanged) { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
if (!mAcquiredConnections.isEmpty()) { |
| 321 |
throw new IllegalStateException("Foreign Key Constraints cannot " |
| 322 |
+ "be enabled or disabled while there are transactions in " |
| 323 |
+ "progress. Finish all transactions and release all active " |
| 324 |
+ "database connections first."); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
boolean onlyCompatWalChanged = (mConfiguration.openFlags ^ configuration.openFlags) |
| 331 |
== SQLiteDatabase.ENABLE_LEGACY_COMPATIBILITY_WAL; |
| 332 |
|
| 333 |
if (!onlyCompatWalChanged && mConfiguration.openFlags != configuration.openFlags) { |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if (walModeChanged) { |
| 338 |
closeAvailableConnectionsAndLogExceptionsLocked(); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
SQLiteConnection newPrimaryConnection = openConnectionLocked(configuration, |
| 346 |
true ); |
| 347 |
|
| 348 |
closeAvailableConnectionsAndLogExceptionsLocked(); |
| 349 |
discardAcquiredConnectionsLocked(); |
| 350 |
|
| 351 |
mAvailablePrimaryConnection = newPrimaryConnection; |
| 352 |
mConfiguration.updateParametersFrom(configuration); |
| 353 |
setMaxConnectionPoolSizeLocked(); |
| 354 |
} else { |
| 355 |
|
| 356 |
mConfiguration.updateParametersFrom(configuration); |
| 357 |
setMaxConnectionPoolSizeLocked(); |
| 358 |
|
| 359 |
closeExcessConnectionsAndLogExceptionsLocked(); |
| 360 |
reconfigureAllConnectionsLocked(); |
| 361 |
} |
| 362 |
|
| 363 |
wakeConnectionWaitersLocked(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public SQLiteConnection acquireConnection(String sql, int connectionFlags, |
| 386 |
CancellationSignal cancellationSignal) { |
| 387 |
SQLiteConnection con = waitForConnection(sql, connectionFlags, cancellationSignal); |
| 388 |
synchronized (mLock) { |
| 389 |
if (mIdleConnectionHandler != null) { |
| 390 |
mIdleConnectionHandler.connectionAcquired(con); |
| 391 |
} |
| 392 |
} |
| 393 |
return con; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public void releaseConnection(SQLiteConnection connection) { |
| 409 |
synchronized (mLock) { |
| 410 |
if (mIdleConnectionHandler != null) { |
| 411 |
mIdleConnectionHandler.connectionReleased(connection); |
| 412 |
} |
| 413 |
AcquiredConnectionStatus status = mAcquiredConnections.remove(connection); |
| 414 |
if (status == null) { |
| 415 |
throw new IllegalStateException("Cannot perform this operation " |
| 416 |
+ "because the specified connection was not acquired " |
| 417 |
+ "from this pool or has already been released."); |
| 418 |
} |
| 419 |
|
| 420 |
if (!mIsOpen) { |
| 421 |
closeConnectionAndLogExceptionsLocked(connection); |
| 422 |
} else if (connection.isPrimaryConnection()) { |
| 423 |
if (recycleConnectionLocked(connection, status)) { |
| 424 |
assert mAvailablePrimaryConnection == null; |
| 425 |
mAvailablePrimaryConnection = connection; |
| 426 |
} |
| 427 |
wakeConnectionWaitersLocked(); |
| 428 |
} else if (mAvailableNonPrimaryConnections.size() >= mMaxConnectionPoolSize - 1) { |
| 429 |
closeConnectionAndLogExceptionsLocked(connection); |
| 430 |
} else { |
| 431 |
if (recycleConnectionLocked(connection, status)) { |
| 432 |
mAvailableNonPrimaryConnections.add(connection); |
| 433 |
} |
| 434 |
wakeConnectionWaitersLocked(); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
@GuardedBy("mLock") |
| 441 |
private boolean recycleConnectionLocked(SQLiteConnection connection, |
| 442 |
AcquiredConnectionStatus status) { |
| 443 |
if (status == AcquiredConnectionStatus.RECONFIGURE) { |
| 444 |
try { |
| 445 |
connection.reconfigure(mConfiguration); |
| 446 |
} catch (RuntimeException ex) { |
| 447 |
Log.e(TAG, "Failed to reconfigure released connection, closing it: " |
| 448 |
+ connection, ex); |
| 449 |
status = AcquiredConnectionStatus.DISCARD; |
| 450 |
} |
| 451 |
} |
| 452 |
if (status == AcquiredConnectionStatus.DISCARD) { |
| 453 |
closeConnectionAndLogExceptionsLocked(connection); |
| 454 |
return false; |
| 455 |
} |
| 456 |
return true; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public boolean shouldYieldConnection(SQLiteConnection connection, int connectionFlags) { |
| 471 |
synchronized (mLock) { |
| 472 |
if (!mAcquiredConnections.containsKey(connection)) { |
| 473 |
throw new IllegalStateException("Cannot perform this operation " |
| 474 |
+ "because the specified connection was not acquired " |
| 475 |
+ "from this pool or has already been released."); |
| 476 |
} |
| 477 |
|
| 478 |
if (!mIsOpen) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
|
| 482 |
return isSessionBlockingImportantConnectionWaitersLocked( |
| 483 |
connection.isPrimaryConnection(), connectionFlags); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public void collectDbStats(ArrayList<DbStats> dbStatsList) { |
| 493 |
synchronized (mLock) { |
| 494 |
if (mAvailablePrimaryConnection != null) { |
| 495 |
mAvailablePrimaryConnection.collectDbStats(dbStatsList); |
| 496 |
} |
| 497 |
|
| 498 |
for (SQLiteConnection connection : mAvailableNonPrimaryConnections) { |
| 499 |
connection.collectDbStats(dbStatsList); |
| 500 |
} |
| 501 |
|
| 502 |
for (SQLiteConnection connection : mAcquiredConnections.keySet()) { |
| 503 |
connection.collectDbStatsUnsafe(dbStatsList); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
private SQLiteConnection openConnectionLocked(SQLiteDatabaseConfiguration configuration, |
| 510 |
boolean primaryConnection) { |
| 511 |
final int connectionId = mNextConnectionId++; |
| 512 |
return SQLiteConnection.open(this, configuration, |
| 513 |
connectionId, primaryConnection); |
| 514 |
} |
| 515 |
|
| 516 |
void onConnectionLeaked() { |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
Log.w(TAG, "A SQLiteConnection object for database '" |
| 538 |
+ mConfiguration.label + "' was leaked! Please fix your application " |
| 539 |
+ "to end transactions in progress properly and to close the database " |
| 540 |
+ "when it is no longer needed."); |
| 541 |
|
| 542 |
mConnectionLeaked.set(true); |
| 543 |
} |
| 544 |
|
| 545 |
void onStatementExecuted(long executionTimeMs) { |
| 546 |
mTotalStatementsTime.addAndGet(executionTimeMs); |
| 547 |
mTotalStatementsCount.incrementAndGet(); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
@GuardedBy("mLock") |
| 552 |
private void closeAvailableConnectionsAndLogExceptionsLocked() { |
| 553 |
closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked(); |
| 554 |
|
| 555 |
if (mAvailablePrimaryConnection != null) { |
| 556 |
closeConnectionAndLogExceptionsLocked(mAvailablePrimaryConnection); |
| 557 |
mAvailablePrimaryConnection = null; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
@GuardedBy("mLock") |
| 563 |
private boolean closeAvailableConnectionLocked(int connectionId) { |
| 564 |
final int count = mAvailableNonPrimaryConnections.size(); |
| 565 |
for (int i = count - 1; i >= 0; i--) { |
| 566 |
SQLiteConnection c = mAvailableNonPrimaryConnections.get(i); |
| 567 |
if (c.getConnectionId() == connectionId) { |
| 568 |
closeConnectionAndLogExceptionsLocked(c); |
| 569 |
mAvailableNonPrimaryConnections.remove(i); |
| 570 |
return true; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
if (mAvailablePrimaryConnection != null |
| 575 |
&& mAvailablePrimaryConnection.getConnectionId() == connectionId) { |
| 576 |
closeConnectionAndLogExceptionsLocked(mAvailablePrimaryConnection); |
| 577 |
mAvailablePrimaryConnection = null; |
| 578 |
return true; |
| 579 |
} |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
@GuardedBy("mLock") |
| 585 |
private void closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked() { |
| 586 |
final int count = mAvailableNonPrimaryConnections.size(); |
| 587 |
for (int i = 0; i < count; i++) { |
| 588 |
closeConnectionAndLogExceptionsLocked(mAvailableNonPrimaryConnections.get(i)); |
| 589 |
} |
| 590 |
mAvailableNonPrimaryConnections.clear(); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
void closeAvailableNonPrimaryConnectionsAndLogExceptions() { |
| 598 |
synchronized (mLock) { |
| 599 |
closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
@GuardedBy("mLock") |
| 605 |
private void closeExcessConnectionsAndLogExceptionsLocked() { |
| 606 |
int availableCount = mAvailableNonPrimaryConnections.size(); |
| 607 |
while (availableCount-- > mMaxConnectionPoolSize - 1) { |
| 608 |
SQLiteConnection connection = |
| 609 |
mAvailableNonPrimaryConnections.remove(availableCount); |
| 610 |
closeConnectionAndLogExceptionsLocked(connection); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
@GuardedBy("mLock") |
| 616 |
private void closeConnectionAndLogExceptionsLocked(SQLiteConnection connection) { |
| 617 |
try { |
| 618 |
connection.close(); |
| 619 |
if (mIdleConnectionHandler != null) { |
| 620 |
mIdleConnectionHandler.connectionClosed(connection); |
| 621 |
} |
| 622 |
} catch (RuntimeException ex) { |
| 623 |
Log.e(TAG, "Failed to close connection, its fate is now in the hands " |
| 624 |
+ "of the merciful GC: " + connection, ex); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
private void discardAcquiredConnectionsLocked() { |
| 630 |
markAcquiredConnectionsLocked(AcquiredConnectionStatus.DISCARD); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
@GuardedBy("mLock") |
| 635 |
private void reconfigureAllConnectionsLocked() { |
| 636 |
if (mAvailablePrimaryConnection != null) { |
| 637 |
try { |
| 638 |
mAvailablePrimaryConnection.reconfigure(mConfiguration); |
| 639 |
} catch (RuntimeException ex) { |
| 640 |
Log.e(TAG, "Failed to reconfigure available primary connection, closing it: " |
| 641 |
+ mAvailablePrimaryConnection, ex); |
| 642 |
closeConnectionAndLogExceptionsLocked(mAvailablePrimaryConnection); |
| 643 |
mAvailablePrimaryConnection = null; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
int count = mAvailableNonPrimaryConnections.size(); |
| 648 |
for (int i = 0; i < count; i++) { |
| 649 |
final SQLiteConnection connection = mAvailableNonPrimaryConnections.get(i); |
| 650 |
try { |
| 651 |
connection.reconfigure(mConfiguration); |
| 652 |
} catch (RuntimeException ex) { |
| 653 |
Log.e(TAG, "Failed to reconfigure available non-primary connection, closing it: " |
| 654 |
+ connection, ex); |
| 655 |
closeConnectionAndLogExceptionsLocked(connection); |
| 656 |
mAvailableNonPrimaryConnections.remove(i--); |
| 657 |
count -= 1; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
markAcquiredConnectionsLocked(AcquiredConnectionStatus.RECONFIGURE); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
private void markAcquiredConnectionsLocked(AcquiredConnectionStatus status) { |
| 666 |
if (!mAcquiredConnections.isEmpty()) { |
| 667 |
ArrayList<SQLiteConnection> keysToUpdate = new ArrayList<SQLiteConnection>( |
| 668 |
mAcquiredConnections.size()); |
| 669 |
for (Map.Entry<SQLiteConnection, AcquiredConnectionStatus> entry |
| 670 |
: mAcquiredConnections.entrySet()) { |
| 671 |
AcquiredConnectionStatus oldStatus = entry.getValue(); |
| 672 |
if (status != oldStatus |
| 673 |
&& oldStatus != AcquiredConnectionStatus.DISCARD) { |
| 674 |
keysToUpdate.add(entry.getKey()); |
| 675 |
} |
| 676 |
} |
| 677 |
final int updateCount = keysToUpdate.size(); |
| 678 |
for (int i = 0; i < updateCount; i++) { |
| 679 |
mAcquiredConnections.put(keysToUpdate.get(i), status); |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
private SQLiteConnection waitForConnection(String sql, int connectionFlags, |
| 686 |
CancellationSignal cancellationSignal) { |
| 687 |
final boolean wantPrimaryConnection = |
| 688 |
(connectionFlags & CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY) != 0; |
| 689 |
|
| 690 |
final ConnectionWaiter waiter; |
| 691 |
final int nonce; |
| 692 |
synchronized (mLock) { |
| 693 |
throwIfClosedLocked(); |
| 694 |
|
| 695 |
|
| 696 |
if (cancellationSignal != null) { |
| 697 |
cancellationSignal.throwIfCanceled(); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
SQLiteConnection connection = null; |
| 702 |
if (!wantPrimaryConnection) { |
| 703 |
connection = tryAcquireNonPrimaryConnectionLocked( |
| 704 |
sql, connectionFlags); |
| 705 |
} |
| 706 |
if (connection == null) { |
| 707 |
connection = tryAcquirePrimaryConnectionLocked(connectionFlags); |
| 708 |
} |
| 709 |
if (connection != null) { |
| 710 |
return connection; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
final int priority = getPriority(connectionFlags); |
| 715 |
final long startTime = SystemClock.uptimeMillis(); |
| 716 |
waiter = obtainConnectionWaiterLocked(Thread.currentThread(), startTime, |
| 717 |
priority, wantPrimaryConnection, sql, connectionFlags); |
| 718 |
ConnectionWaiter predecessor = null; |
| 719 |
ConnectionWaiter successor = mConnectionWaiterQueue; |
| 720 |
while (successor != null) { |
| 721 |
if (priority > successor.mPriority) { |
| 722 |
waiter.mNext = successor; |
| 723 |
break; |
| 724 |
} |
| 725 |
predecessor = successor; |
| 726 |
successor = successor.mNext; |
| 727 |
} |
| 728 |
if (predecessor != null) { |
| 729 |
predecessor.mNext = waiter; |
| 730 |
} else { |
| 731 |
mConnectionWaiterQueue = waiter; |
| 732 |
} |
| 733 |
|
| 734 |
nonce = waiter.mNonce; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
if (cancellationSignal != null) { |
| 739 |
cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() { |
| 740 |
@Override |
| 741 |
public void onCancel() { |
| 742 |
synchronized (mLock) { |
| 743 |
if (waiter.mNonce == nonce) { |
| 744 |
cancelConnectionWaiterLocked(waiter); |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
}); |
| 749 |
} |
| 750 |
try { |
| 751 |
|
| 752 |
|
| 753 |
long busyTimeoutMillis = CONNECTION_POOL_BUSY_MILLIS; |
| 754 |
long nextBusyTimeoutTime = waiter.mStartTime + busyTimeoutMillis; |
| 755 |
for (;;) { |
| 756 |
|
| 757 |
if (mConnectionLeaked.compareAndSet(true, false)) { |
| 758 |
synchronized (mLock) { |
| 759 |
wakeConnectionWaitersLocked(); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
LockSupport.parkNanos(this, busyTimeoutMillis * 1000000L); |
| 765 |
|
| 766 |
|
| 767 |
Thread.interrupted(); |
| 768 |
|
| 769 |
|
| 770 |
synchronized (mLock) { |
| 771 |
throwIfClosedLocked(); |
| 772 |
|
| 773 |
final SQLiteConnection connection = waiter.mAssignedConnection; |
| 774 |
final RuntimeException ex = waiter.mException; |
| 775 |
if (connection != null || ex != null) { |
| 776 |
recycleConnectionWaiterLocked(waiter); |
| 777 |
if (connection != null) { |
| 778 |
return connection; |
| 779 |
} |
| 780 |
throw ex; |
| 781 |
} |
| 782 |
|
| 783 |
final long now = SystemClock.uptimeMillis(); |
| 784 |
if (now < nextBusyTimeoutTime) { |
| 785 |
busyTimeoutMillis = now - nextBusyTimeoutTime; |
| 786 |
} else { |
| 787 |
logConnectionPoolBusyLocked(now - waiter.mStartTime, connectionFlags); |
| 788 |
busyTimeoutMillis = CONNECTION_POOL_BUSY_MILLIS; |
| 789 |
nextBusyTimeoutTime = now + busyTimeoutMillis; |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
} finally { |
| 794 |
|
| 795 |
if (cancellationSignal != null) { |
| 796 |
cancellationSignal.setOnCancelListener(null); |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
@GuardedBy("mLock") |
| 803 |
private void cancelConnectionWaiterLocked(ConnectionWaiter waiter) { |
| 804 |
if (waiter.mAssignedConnection != null || waiter.mException != null) { |
| 805 |
|
| 806 |
return; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
ConnectionWaiter predecessor = null; |
| 811 |
ConnectionWaiter current = mConnectionWaiterQueue; |
| 812 |
while (current != waiter) { |
| 813 |
assert current != null; |
| 814 |
predecessor = current; |
| 815 |
current = current.mNext; |
| 816 |
} |
| 817 |
if (predecessor != null) { |
| 818 |
predecessor.mNext = waiter.mNext; |
| 819 |
} else { |
| 820 |
mConnectionWaiterQueue = waiter.mNext; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
waiter.mException = new OperationCanceledException(); |
| 825 |
LockSupport.unpark(waiter.mThread); |
| 826 |
|
| 827 |
|
| 828 |
wakeConnectionWaitersLocked(); |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
private void logConnectionPoolBusyLocked(long waitMillis, int connectionFlags) { |
| 833 |
final Thread thread = Thread.currentThread(); |
| 834 |
StringBuilder msg = new StringBuilder(); |
| 835 |
msg.append("The connection pool for database '").append(mConfiguration.label); |
| 836 |
msg.append("' has been unable to grant a connection to thread "); |
| 837 |
msg.append(thread.getId()).append(" (").append(thread.getName()).append(") "); |
| 838 |
msg.append("with flags 0x").append(Integer.toHexString(connectionFlags)); |
| 839 |
msg.append(" for ").append(waitMillis * 0.001f).append(" seconds.\n"); |
| 840 |
|
| 841 |
ArrayList<String> requests = new ArrayList<String>(); |
| 842 |
int activeConnections = 0; |
| 843 |
int idleConnections = 0; |
| 844 |
if (!mAcquiredConnections.isEmpty()) { |
| 845 |
for (SQLiteConnection connection : mAcquiredConnections.keySet()) { |
| 846 |
String description = connection.describeCurrentOperationUnsafe(); |
| 847 |
if (description != null) { |
| 848 |
requests.add(description); |
| 849 |
activeConnections += 1; |
| 850 |
} else { |
| 851 |
idleConnections += 1; |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
int availableConnections = mAvailableNonPrimaryConnections.size(); |
| 856 |
if (mAvailablePrimaryConnection != null) { |
| 857 |
availableConnections += 1; |
| 858 |
} |
| 859 |
|
| 860 |
msg.append("Connections: ").append(activeConnections).append(" active, "); |
| 861 |
msg.append(idleConnections).append(" idle, "); |
| 862 |
msg.append(availableConnections).append(" available.\n"); |
| 863 |
|
| 864 |
if (!requests.isEmpty()) { |
| 865 |
msg.append("\nRequests in progress:\n"); |
| 866 |
for (String request : requests) { |
| 867 |
msg.append(" ").append(request).append("\n"); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
Log.w(TAG, msg.toString()); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
@GuardedBy("mLock") |
| 876 |
private void wakeConnectionWaitersLocked() { |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
ConnectionWaiter predecessor = null; |
| 881 |
ConnectionWaiter waiter = mConnectionWaiterQueue; |
| 882 |
boolean primaryConnectionNotAvailable = false; |
| 883 |
boolean nonPrimaryConnectionNotAvailable = false; |
| 884 |
while (waiter != null) { |
| 885 |
boolean unpark = false; |
| 886 |
if (!mIsOpen) { |
| 887 |
unpark = true; |
| 888 |
} else { |
| 889 |
try { |
| 890 |
SQLiteConnection connection = null; |
| 891 |
if (!waiter.mWantPrimaryConnection && !nonPrimaryConnectionNotAvailable) { |
| 892 |
connection = tryAcquireNonPrimaryConnectionLocked( |
| 893 |
waiter.mSql, waiter.mConnectionFlags); |
| 894 |
if (connection == null) { |
| 895 |
nonPrimaryConnectionNotAvailable = true; |
| 896 |
} |
| 897 |
} |
| 898 |
if (connection == null && !primaryConnectionNotAvailable) { |
| 899 |
connection = tryAcquirePrimaryConnectionLocked( |
| 900 |
waiter.mConnectionFlags); |
| 901 |
if (connection == null) { |
| 902 |
primaryConnectionNotAvailable = true; |
| 903 |
} |
| 904 |
} |
| 905 |
if (connection != null) { |
| 906 |
waiter.mAssignedConnection = connection; |
| 907 |
unpark = true; |
| 908 |
} else if (nonPrimaryConnectionNotAvailable && primaryConnectionNotAvailable) { |
| 909 |
|
| 910 |
|
| 911 |
break; |
| 912 |
} |
| 913 |
} catch (RuntimeException ex) { |
| 914 |
|
| 915 |
waiter.mException = ex; |
| 916 |
unpark = true; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
final ConnectionWaiter successor = waiter.mNext; |
| 921 |
if (unpark) { |
| 922 |
if (predecessor != null) { |
| 923 |
predecessor.mNext = successor; |
| 924 |
} else { |
| 925 |
mConnectionWaiterQueue = successor; |
| 926 |
} |
| 927 |
waiter.mNext = null; |
| 928 |
|
| 929 |
LockSupport.unpark(waiter.mThread); |
| 930 |
} else { |
| 931 |
predecessor = waiter; |
| 932 |
} |
| 933 |
waiter = successor; |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
@GuardedBy("mLock") |
| 939 |
private SQLiteConnection tryAcquirePrimaryConnectionLocked(int connectionFlags) { |
| 940 |
|
| 941 |
SQLiteConnection connection = mAvailablePrimaryConnection; |
| 942 |
if (connection != null) { |
| 943 |
mAvailablePrimaryConnection = null; |
| 944 |
finishAcquireConnectionLocked(connection, connectionFlags); |
| 945 |
return connection; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
for (SQLiteConnection acquiredConnection : mAcquiredConnections.keySet()) { |
| 950 |
if (acquiredConnection.isPrimaryConnection()) { |
| 951 |
return null; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
connection = openConnectionLocked(mConfiguration, |
| 958 |
true ); |
| 959 |
finishAcquireConnectionLocked(connection, connectionFlags); |
| 960 |
return connection; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
@GuardedBy("mLock") |
| 965 |
private SQLiteConnection tryAcquireNonPrimaryConnectionLocked( |
| 966 |
String sql, int connectionFlags) { |
| 967 |
|
| 968 |
SQLiteConnection connection; |
| 969 |
final int availableCount = mAvailableNonPrimaryConnections.size(); |
| 970 |
if (availableCount > 1 && sql != null) { |
| 971 |
|
| 972 |
|
| 973 |
for (int i = 0; i < availableCount; i++) { |
| 974 |
connection = mAvailableNonPrimaryConnections.get(i); |
| 975 |
if (connection.isPreparedStatementInCache(sql)) { |
| 976 |
mAvailableNonPrimaryConnections.remove(i); |
| 977 |
finishAcquireConnectionLocked(connection, connectionFlags); |
| 978 |
return connection; |
| 979 |
} |
| 980 |
} |
| 981 |
} |
| 982 |
if (availableCount > 0) { |
| 983 |
|
| 984 |
connection = mAvailableNonPrimaryConnections.remove(availableCount - 1); |
| 985 |
finishAcquireConnectionLocked(connection, connectionFlags); |
| 986 |
return connection; |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
int openConnections = mAcquiredConnections.size(); |
| 991 |
if (mAvailablePrimaryConnection != null) { |
| 992 |
openConnections += 1; |
| 993 |
} |
| 994 |
if (openConnections >= mMaxConnectionPoolSize) { |
| 995 |
return null; |
| 996 |
} |
| 997 |
connection = openConnectionLocked(mConfiguration, |
| 998 |
false ); |
| 999 |
finishAcquireConnectionLocked(connection, connectionFlags); |
| 1000 |
return connection; |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
@GuardedBy("mLock") |
| 1005 |
private void finishAcquireConnectionLocked(SQLiteConnection connection, int connectionFlags) { |
| 1006 |
try { |
| 1007 |
final boolean readOnly = (connectionFlags & CONNECTION_FLAG_READ_ONLY) != 0; |
| 1008 |
connection.setOnlyAllowReadOnlyOperations(readOnly); |
| 1009 |
|
| 1010 |
mAcquiredConnections.put(connection, AcquiredConnectionStatus.NORMAL); |
| 1011 |
} catch (RuntimeException ex) { |
| 1012 |
Log.e(TAG, "Failed to prepare acquired connection for session, closing it: " |
| 1013 |
+ connection +", connectionFlags=" + connectionFlags); |
| 1014 |
closeConnectionAndLogExceptionsLocked(connection); |
| 1015 |
throw ex; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
private boolean isSessionBlockingImportantConnectionWaitersLocked( |
| 1020 |
boolean holdingPrimaryConnection, int connectionFlags) { |
| 1021 |
ConnectionWaiter waiter = mConnectionWaiterQueue; |
| 1022 |
if (waiter != null) { |
| 1023 |
final int priority = getPriority(connectionFlags); |
| 1024 |
do { |
| 1025 |
|
| 1026 |
if (priority > waiter.mPriority) { |
| 1027 |
break; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
if (holdingPrimaryConnection || !waiter.mWantPrimaryConnection) { |
| 1034 |
return true; |
| 1035 |
} |
| 1036 |
|
| 1037 |
waiter = waiter.mNext; |
| 1038 |
} while (waiter != null); |
| 1039 |
} |
| 1040 |
return false; |
| 1041 |
} |
| 1042 |
|
| 1043 |
private static int getPriority(int connectionFlags) { |
| 1044 |
return (connectionFlags & CONNECTION_FLAG_INTERACTIVE) != 0 ? 1 : 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
private void setMaxConnectionPoolSizeLocked() { |
| 1048 |
if (mConfiguration.resolveJournalMode().equalsIgnoreCase(SQLiteDatabase.JOURNAL_MODE_WAL)) { |
| 1049 |
mMaxConnectionPoolSize = SQLiteGlobal.getWALConnectionPoolSize(); |
| 1050 |
} else { |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
mMaxConnectionPoolSize = 1; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
@VisibleForTesting |
| 1064 |
public void setupIdleConnectionHandler(Looper looper, long timeoutMs) { |
| 1065 |
synchronized (mLock) { |
| 1066 |
mIdleConnectionHandler = new IdleConnectionHandler(looper, timeoutMs); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
void disableIdleConnectionHandler() { |
| 1071 |
synchronized (mLock) { |
| 1072 |
mIdleConnectionHandler = null; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
private void throwIfClosedLocked() { |
| 1077 |
if (!mIsOpen) { |
| 1078 |
throw new IllegalStateException("Cannot perform this operation " |
| 1079 |
+ "because the connection pool has been closed."); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
private ConnectionWaiter obtainConnectionWaiterLocked(Thread thread, long startTime, |
| 1084 |
int priority, boolean wantPrimaryConnection, String sql, int connectionFlags) { |
| 1085 |
ConnectionWaiter waiter = mConnectionWaiterPool; |
| 1086 |
if (waiter != null) { |
| 1087 |
mConnectionWaiterPool = waiter.mNext; |
| 1088 |
waiter.mNext = null; |
| 1089 |
} else { |
| 1090 |
waiter = new ConnectionWaiter(); |
| 1091 |
} |
| 1092 |
waiter.mThread = thread; |
| 1093 |
waiter.mStartTime = startTime; |
| 1094 |
waiter.mPriority = priority; |
| 1095 |
waiter.mWantPrimaryConnection = wantPrimaryConnection; |
| 1096 |
waiter.mSql = sql; |
| 1097 |
waiter.mConnectionFlags = connectionFlags; |
| 1098 |
return waiter; |
| 1099 |
} |
| 1100 |
|
| 1101 |
private void recycleConnectionWaiterLocked(ConnectionWaiter waiter) { |
| 1102 |
waiter.mNext = mConnectionWaiterPool; |
| 1103 |
waiter.mThread = null; |
| 1104 |
waiter.mSql = null; |
| 1105 |
waiter.mAssignedConnection = null; |
| 1106 |
waiter.mException = null; |
| 1107 |
waiter.mNonce += 1; |
| 1108 |
mConnectionWaiterPool = waiter; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
public void dump(Printer printer, boolean verbose, ArraySet<String> directories) { |
| 1118 |
Printer indentedPrinter = PrefixPrinter.create(printer, " "); |
| 1119 |
synchronized (mLock) { |
| 1120 |
if (directories != null) { |
| 1121 |
directories.add(new File(mConfiguration.path).getParent()); |
| 1122 |
} |
| 1123 |
boolean isCompatibilityWalEnabled = mConfiguration.isLegacyCompatibilityWalEnabled(); |
| 1124 |
printer.println("Connection pool for " + mConfiguration.path + ":"); |
| 1125 |
printer.println(" Open: " + mIsOpen); |
| 1126 |
printer.println(" Max connections: " + mMaxConnectionPoolSize); |
| 1127 |
printer.println(" Total execution time (ms): " + mTotalStatementsTime); |
| 1128 |
printer.println(" Total statements executed: " + mTotalStatementsCount); |
| 1129 |
if (mTotalStatementsCount.get() > 0) { |
| 1130 |
|
| 1131 |
printer.println(" Average time per statement (ms): " |
| 1132 |
+ mTotalStatementsTime.get() / mTotalStatementsCount.get()); |
| 1133 |
} |
| 1134 |
printer.println(" Configuration: openFlags=" + mConfiguration.openFlags |
| 1135 |
+ ", isLegacyCompatibilityWalEnabled=" + isCompatibilityWalEnabled |
| 1136 |
+ ", journalMode=" + TextUtils.emptyIfNull(mConfiguration.resolveJournalMode()) |
| 1137 |
+ ", syncMode=" + TextUtils.emptyIfNull(mConfiguration.resolveSyncMode())); |
| 1138 |
printer.println(" IsReadOnlyDatabase=" + mConfiguration.isReadOnlyDatabase()); |
| 1139 |
|
| 1140 |
if (isCompatibilityWalEnabled) { |
| 1141 |
printer.println(" Compatibility WAL enabled: wal_syncmode=" |
| 1142 |
+ SQLiteCompatibilityWalFlags.getWALSyncMode()); |
| 1143 |
} |
| 1144 |
if (mConfiguration.isLookasideConfigSet()) { |
| 1145 |
printer.println(" Lookaside config: sz=" + mConfiguration.lookasideSlotSize |
| 1146 |
+ " cnt=" + mConfiguration.lookasideSlotCount); |
| 1147 |
} |
| 1148 |
if (mConfiguration.idleConnectionTimeoutMs != Long.MAX_VALUE) { |
| 1149 |
printer.println( |
| 1150 |
" Idle connection timeout: " + mConfiguration.idleConnectionTimeoutMs); |
| 1151 |
} |
| 1152 |
printer.println(" Available primary connection:"); |
| 1153 |
if (mAvailablePrimaryConnection != null) { |
| 1154 |
mAvailablePrimaryConnection.dump(indentedPrinter, verbose); |
| 1155 |
} else { |
| 1156 |
indentedPrinter.println("<none>"); |
| 1157 |
} |
| 1158 |
|
| 1159 |
printer.println(" Available non-primary connections:"); |
| 1160 |
if (!mAvailableNonPrimaryConnections.isEmpty()) { |
| 1161 |
final int count = mAvailableNonPrimaryConnections.size(); |
| 1162 |
for (int i = 0; i < count; i++) { |
| 1163 |
mAvailableNonPrimaryConnections.get(i).dump(indentedPrinter, verbose); |
| 1164 |
} |
| 1165 |
} else { |
| 1166 |
indentedPrinter.println("<none>"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
printer.println(" Acquired connections:"); |
| 1170 |
if (!mAcquiredConnections.isEmpty()) { |
| 1171 |
for (Map.Entry<SQLiteConnection, AcquiredConnectionStatus> entry : |
| 1172 |
mAcquiredConnections.entrySet()) { |
| 1173 |
final SQLiteConnection connection = entry.getKey(); |
| 1174 |
connection.dumpUnsafe(indentedPrinter, verbose); |
| 1175 |
indentedPrinter.println(" Status: " + entry.getValue()); |
| 1176 |
} |
| 1177 |
} else { |
| 1178 |
indentedPrinter.println("<none>"); |
| 1179 |
} |
| 1180 |
|
| 1181 |
printer.println(" Connection waiters:"); |
| 1182 |
if (mConnectionWaiterQueue != null) { |
| 1183 |
int i = 0; |
| 1184 |
final long now = SystemClock.uptimeMillis(); |
| 1185 |
for (ConnectionWaiter waiter = mConnectionWaiterQueue; waiter != null; |
| 1186 |
waiter = waiter.mNext, i++) { |
| 1187 |
indentedPrinter.println(i + ": waited for " |
| 1188 |
+ ((now - waiter.mStartTime) * 0.001f) |
| 1189 |
+ " ms - thread=" + waiter.mThread |
| 1190 |
+ ", priority=" + waiter.mPriority |
| 1191 |
+ ", sql='" + waiter.mSql + "'"); |
| 1192 |
} |
| 1193 |
} else { |
| 1194 |
indentedPrinter.println("<none>"); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
public long getTotalStatementsTime() { |
| 1200 |
return mTotalStatementsTime.get(); |
| 1201 |
} |
| 1202 |
|
| 1203 |
public long getTotalStatementsCount() { |
| 1204 |
return mTotalStatementsCount.get(); |
| 1205 |
} |
| 1206 |
|
| 1207 |
@Override |
| 1208 |
public String toString() { |
| 1209 |
return "SQLiteConnectionPool: " + mConfiguration.path; |
| 1210 |
} |
| 1211 |
|
| 1212 |
public String getPath() { |
| 1213 |
return mConfiguration.path; |
| 1214 |
} |
| 1215 |
|
| 1216 |
private static final class ConnectionWaiter { |
| 1217 |
public ConnectionWaiter mNext; |
| 1218 |
public Thread mThread; |
| 1219 |
public long mStartTime; |
| 1220 |
public int mPriority; |
| 1221 |
public boolean mWantPrimaryConnection; |
| 1222 |
public String mSql; |
| 1223 |
public int mConnectionFlags; |
| 1224 |
public SQLiteConnection mAssignedConnection; |
| 1225 |
public RuntimeException mException; |
| 1226 |
public int mNonce; |
| 1227 |
} |
| 1228 |
|
| 1229 |
private class IdleConnectionHandler extends Handler { |
| 1230 |
private final long mTimeout; |
| 1231 |
|
| 1232 |
IdleConnectionHandler(Looper looper, long timeout) { |
| 1233 |
super(looper); |
| 1234 |
mTimeout = timeout; |
| 1235 |
} |
| 1236 |
|
| 1237 |
@Override |
| 1238 |
public void handleMessage(Message msg) { |
| 1239 |
|
| 1240 |
synchronized (mLock) { |
| 1241 |
if (this != mIdleConnectionHandler) { |
| 1242 |
return; |
| 1243 |
} |
| 1244 |
if (closeAvailableConnectionLocked(msg.what)) { |
| 1245 |
if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1246 |
Log.d(TAG, "Closed idle connection " + mConfiguration.label + " " + msg.what |
| 1247 |
+ " after " + mTimeout); |
| 1248 |
} |
| 1249 |
} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
void connectionReleased(SQLiteConnection con) { |
| 1254 |
sendEmptyMessageDelayed(con.getConnectionId(), mTimeout); |
| 1255 |
} |
| 1256 |
|
| 1257 |
void connectionAcquired(SQLiteConnection con) { |
| 1258 |
|
| 1259 |
removeMessages(con.getConnectionId()); |
| 1260 |
} |
| 1261 |
|
| 1262 |
void connectionClosed(SQLiteConnection con) { |
| 1263 |
removeMessages(con.getConnectionId()); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
} |