Skip to content

Commit f2207a5

Browse files
committed
Guard SQLiteDriver.hasConnectionPool for sqlite 2.5.x delegates
1 parent d4ab6b7 commit f2207a5

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add `SentrySQLiteDriver` to `sentry-android-sqlite` for instrumenting AndroidX's `SQLiteDriver` ([#5466](https://github.com/getsentry/sentry-java/pull/5466))
88
- Automatically generates spans for all SQLite statements
99
- To use it, pass your `SQLiteDriver` to `SentrySQLiteDriver.create(...)`
10+
- You'll need `androidx.sqlite:sqlite` (2.5.0+) on your app's classpath (Room usually provides it for you). androidx.sqlite 2.6.0+ requires minSdk 23.
1011
- See https://docs.sentry.io/platforms/android/integrations/room-and-sqlite/ for more details, including info about migrating from `SentrySupportSQLiteOpenHelper`
1112

1213
## 8.43.1

sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ public class SentrySQLiteDriver private constructor(private val delegate: SQLite
3636
}
3737

3838
override val hasConnectionPool: Boolean
39-
get() = delegate.hasConnectionPool
39+
get() =
40+
try {
41+
delegate.hasConnectionPool
42+
} catch (_: LinkageError) {
43+
// Delegates on androidx.sqlite < 2.6.0 won't have a hasConnectionPool property.
44+
false
45+
}
4046

4147
@Suppress("TooGenericExceptionCaught")
4248
override fun open(fileName: String): SQLiteConnection {
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:tools="http://schemas.android.com/tools">
33

4-
<!-- Override b/c androidx.sqlite 2.6.0 raised its minSdk to 23, but this test module is at minSdk 21. -->
5-
<uses-sdk tools:overrideLibrary="androidx.sqlite.db"/>
4+
<!-- androidx.sqlite 2.6.0+ raised minSdk to 23; this module still targets 21. -->
5+
<uses-sdk
6+
tools:overrideLibrary="
7+
androidx.sqlite.db,
8+
androidx.sqlite.db.framework,
9+
androidx.sqlite.driver.bundled,
10+
androidx.sqlite.sqlite.anchor,
11+
androidx.sqlite.sqlite.bundled.anchor,
12+
androidx.sqlite.sqlite.framework.anchor" />
613
</manifest>

sentry-android-sqlite/src/test/java/io/sentry/sqlite/SentrySQLiteDriverTest.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.sentry.SpanDataConvention
1212
import io.sentry.TransactionContext
1313
import kotlin.test.Test
1414
import kotlin.test.assertEquals
15+
import kotlin.test.assertFailsWith
1516
import kotlin.test.assertFalse
1617
import kotlin.test.assertIs
1718
import kotlin.test.assertNotNull
@@ -63,6 +64,27 @@ class SentrySQLiteDriverTest {
6364
assertSame(wrapped, doubleWrapped)
6465
}
6566

67+
@Test
68+
fun `hasConnectionPool forwards delegate value when supported`() {
69+
whenever(fixture.mockDriver.hasConnectionPool).thenReturn(true)
70+
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
71+
assertTrue(sut.hasConnectionPool)
72+
}
73+
74+
@Test
75+
fun `hasConnectionPool returns false when delegate throws LinkageError`() {
76+
whenever(fixture.mockDriver.hasConnectionPool).thenThrow(AbstractMethodError())
77+
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
78+
assertFalse(sut.hasConnectionPool)
79+
}
80+
81+
@Test
82+
fun `hasConnectionPool does not catch non-LinkageErrors`() {
83+
whenever(fixture.mockDriver.hasConnectionPool).thenThrow(IllegalStateException())
84+
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
85+
assertFailsWith<IllegalStateException> { sut.hasConnectionPool }
86+
}
87+
6688
@Test
6789
fun `open returns SentrySQLiteConnection wrapping delegate if wrapping succeeds`() {
6890
val driver = fixture.getSut("myapp.db")
@@ -89,20 +111,6 @@ class SentrySQLiteDriverTest {
89111
}
90112
}
91113

92-
@Test
93-
fun `all calls are propagated to the delegate`() {
94-
val sut = fixture.getSut("myapp.db")
95-
96-
whenever(fixture.mockDriver.hasConnectionPool).thenReturn(true)
97-
assertTrue(sut.hasConnectionPool)
98-
99-
whenever(fixture.mockDriver.hasConnectionPool).thenReturn(false)
100-
assertFalse(sut.hasConnectionPool)
101-
102-
sut.open("myapp.db")
103-
verify(fixture.mockDriver).open("myapp.db")
104-
}
105-
106114
// Smoke test ensuring all layers are properly wired up.
107115
@Test
108116
fun `full stack produces a span with correct metadata`() {

0 commit comments

Comments
 (0)