Permalink
Browse files

sanitize exceptions

  • Loading branch information...
1 parent 46329d3 commit f1a5d306d3411355e50a767d4f2509ba3d9103d5 Ed Ceaser committed Feb 24, 2010
View
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project name="db" default="package" xmlns:ivy="antlib:org.apache.ivy.ant">
- <description>database library</description>
+<project name="querulous" default="package" xmlns:ivy="antlib:org.apache.ivy.ant">
+ <description>An agreeable way to talk to your database.</description>
<!-- override creation of a jar with a version number in the name -->
<property name="dist.build_integration" value="true" />
@@ -1,8 +1,10 @@
package com.twitter.querulous.database
+import java.sql.{Connection, SQLException}
import java.util.concurrent.{TimeoutException => JTimeoutException, _}
import com.twitter.xrayspecs.Duration
-import java.sql.Connection
+
+class SqlDatabaseTimeoutException(msg: String) extends SQLException(msg)
class TimingOutDatabaseFactory(databaseFactory: DatabaseFactory, poolSize: Int, queueSize: Int, openTimeout: Duration, initialTimeout: Duration) extends DatabaseFactory {
def apply(dbhosts: List[String], dbname: String, username: String, password: String): Database = {
@@ -28,8 +30,8 @@ class TimingOutDatabase(database: Database, dbhosts: List[String], dbname: Strin
executor.execute(future)
future.get(openTimeout.inMillis, TimeUnit.MILLISECONDS)
} catch {
- case e: JTimeoutException => throw new TimeoutException(dbhosts.mkString(",")+"/"+dbname)
- case e: RejectedExecutionException => throw new TimeoutException(dbhosts.mkString(",")+"/"+dbname)
+ case e: JTimeoutException => throw new SqlDatabaseTimeoutException(dbhosts.mkString(",")+"/"+dbname)
+ case e: RejectedExecutionException => throw new SqlDatabaseTimeoutException(dbhosts.mkString(",")+"/"+dbname)
}
}
@@ -4,7 +4,7 @@ import java.sql.{SQLException, Connection}
import com.twitter.xrayspecs.Duration
-class SqlTimeoutException extends SQLException("Query timeout")
+class SqlQueryTimeoutException extends SQLException("Query timeout")
class TimingOutQueryFactory(queryFactory: QueryFactory, timeout: Duration) extends QueryFactory {
def apply(connection: Connection, query: String, params: Any*) = {
@@ -30,7 +30,7 @@ class TimingOutQuery(query: Query, timeout: Duration) extends QueryProxy(query)
}
} catch {
case e: TimeoutException =>
- throw new SqlTimeoutException
+ throw new SqlQueryTimeoutException
}
}
}
@@ -6,7 +6,7 @@ import net.lag.configgy.Configgy
import com.twitter.xrayspecs.Time
import com.twitter.xrayspecs.TimeConversions._
import com.twitter.querulous.database.ApachePoolingDatabaseFactory
-import com.twitter.querulous.query.{SqlQueryFactory, TimingOutQueryFactory, SqlTimeoutException}
+import com.twitter.querulous.query.{SqlQueryFactory, TimingOutQueryFactory, SqlQueryTimeoutException}
import com.twitter.querulous.evaluator.{StandardQueryEvaluatorFactory, QueryEvaluator}
object TimeoutSpec extends Specification {
@@ -44,7 +44,7 @@ object TimeoutSpec extends Specification {
val queryEvaluator2 = timingOutQueryEvaluatorFactory(List("localhost"), "db_test", username, password)
val start = Time.now
- queryEvaluator2.select("SELECT GET_LOCK('padlock', 60) AS rv") { row => row.getInt("rv") } must throwA[SqlTimeoutException]
+ queryEvaluator2.select("SELECT GET_LOCK('padlock', 60) AS rv") { row => row.getInt("rv") } must throwA[SqlQueryTimeoutException]
val end = Time.now
(end - start).inMillis must beCloseTo(timeout.inMillis, 1.second.inMillis)
thread.interrupt()
@@ -3,7 +3,7 @@ package com.twitter.querulous.unit
import java.util.concurrent.{CountDownLatch, TimeUnit}
import java.sql.Connection
import com.twitter.querulous.TimeoutException
-import com.twitter.querulous.database.{Database, TimingOutDatabase}
+import com.twitter.querulous.database.{SqlDatabaseTimeoutException, TimingOutDatabase, Database}
import com.twitter.xrayspecs.Time
import com.twitter.xrayspecs.TimeConversions._
import org.specs.Specification
@@ -30,13 +30,12 @@ object TimingOutDatabaseSpec extends Specification with JMocker with ClassMocker
try {
val epsilon = 100.millis
var start = Time.now
- timingOutDatabase.open() must throwA[TimeoutException]
+ timingOutDatabase.open() must throwA[SqlDatabaseTimeoutException]
var end = Time.now
(end.inMillis - start.inMillis) must beCloseTo(timeout.inMillis, epsilon.inMillis)
} finally {
latch.countDown()
}
}
-
}
}
@@ -1,10 +1,10 @@
package com.twitter.querulous.unit
-import java.sql.{ResultSet, SQLException}
+import java.sql.ResultSet
import org.specs.Specification
import org.specs.mock.{JMocker, ClassMocker}
import com.twitter.querulous.test.FakeQuery
-import com.twitter.querulous.query.{TimingOutQuery, SqlTimeoutException}
+import com.twitter.querulous.query.{TimingOutQuery, SqlQueryTimeoutException}
import com.twitter.xrayspecs.Duration
import com.twitter.xrayspecs.TimeConversions._
import java.util.concurrent.{CountDownLatch, TimeUnit}
@@ -26,7 +26,7 @@ object TimingOutQuerySpec extends Specification with JMocker with ClassMocker {
}
val timingOutQuery = new TimingOutQuery(query, timeout)
- timingOutQuery.select { r => 1 } must throwA[SqlTimeoutException]
+ timingOutQuery.select { r => 1 } must throwA[SqlQueryTimeoutException]
latch.getCount mustEqual 0
}

0 comments on commit f1a5d30

Please sign in to comment.