Skip to content

Commit

Permalink
JDBC DataSource should not need to be closeable (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus authored Oct 7, 2022
1 parent e0a9d3d commit c6e91ed
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion quill-jdbc/src/main/scala/io/getquill/H2JdbcContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.typesafe.config.Config
import io.getquill.context.jdbc.{ JdbcContext, H2JdbcContextBase }
import io.getquill.util.LoadConfig

class H2JdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class H2JdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[H2Dialect, N]
with H2JdbcContextBase[H2Dialect, N] {
override val idiom: H2Dialect = H2Dialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.typesafe.config.Config
import io.getquill.context.jdbc.{ JdbcContext, MysqlJdbcContextBase }
import io.getquill.util.LoadConfig

class MysqlJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class MysqlJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[MySQLDialect, N]
with MysqlJdbcContextBase[MySQLDialect, N] {
override val idiom: MySQLDialect = MySQLDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.getquill.context.jdbc.{ JdbcContext, OracleJdbcContextBase }
import io.getquill.util.LoadConfig
import javax.sql.DataSource

class OracleJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class OracleJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[OracleDialect, N]
with OracleJdbcContextBase[OracleDialect, N] {
override val idiom: OracleDialect = OracleDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.typesafe.config.Config
import io.getquill.context.jdbc.{ JdbcContext, PostgresJdbcContextBase }
import io.getquill.util.LoadConfig

class PostgresJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class PostgresJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[PostgresDialect, N]
with PostgresJdbcContextBase[PostgresDialect, N] {
override val idiom: PostgresDialect = PostgresDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.typesafe.config.Config
import io.getquill.context.jdbc.{ JdbcContext, SqlServerJdbcContextBase }
import io.getquill.util.LoadConfig

class SqlServerJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class SqlServerJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[SQLServerDialect, N]
with SqlServerJdbcContextBase[SQLServerDialect, N] {
override val idiom: SQLServerDialect = SQLServerDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.typesafe.config.Config
import io.getquill.context.jdbc.{ JdbcContext, SqliteJdbcContextBase }
import io.getquill.util.LoadConfig

class SqliteJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource with Closeable)
class SqliteJdbcContext[+N <: NamingStrategy](val naming: N, val dataSource: DataSource)
extends JdbcContext[SqliteDialect, N]
with SqliteJdbcContextBase[SqliteDialect, N] {
override val idiom: SqliteDialect = SqliteDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import scala.util.control.NonFatal
import io.getquill.Quoted
import scala.annotation.targetName
import io.getquill.context.ContextVerbTranslate
import io.getquill.util.ContextLogger

abstract class JdbcContext[+Dialect <: SqlIdiom, +Naming <: NamingStrategy]
extends JdbcContextBase[Dialect, Naming]
with ProtoContextSecundus[Dialect, Naming]
with ContextVerbTranslate[Dialect, Naming]
{

private val logger = ContextLogger(classOf[JdbcContext[_, _]])

// Need to override these with same values as JdbcRunContext because SyncIOMonad imports them. The imported values need to be overridden
override type Result[T] = T
override type RunQueryResult[T] = List[T]
Expand All @@ -32,7 +35,7 @@ abstract class JdbcContext[+Dialect <: SqlIdiom, +Naming <: NamingStrategy]
override protected def context: Runner = ()
def translateContext: TranslateRunner = ()

val dataSource: DataSource with Closeable
val dataSource: DataSource

@targetName("runQueryDefault")
inline def run[T](inline quoted: Quoted[Query[T]]): List[T] = InternalApi.runQueryDefault(quoted)
Expand Down Expand Up @@ -68,7 +71,13 @@ abstract class JdbcContext[+Dialect <: SqlIdiom, +Naming <: NamingStrategy]
finally conn.close()
}

override def close() = dataSource.close()
override def close() =
dataSource match {
case closeable: java.io.Closeable =>
closeable.close()
case _ =>
logger.underlying.warn(s"Could not close the DataSource `$dataSource`. It is not an instance of java.io.Closeable.")
}

def probe(sql: String) =
Try {
Expand Down

0 comments on commit c6e91ed

Please sign in to comment.