This repository has been archived by the owner on May 22, 2019. It is now read-only.
/
SqlExceptionWrappingProxy.scala
85 lines (79 loc) · 3.3 KB
/
SqlExceptionWrappingProxy.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
83
84
85
package com.twitter.gizzard.proxy
import java.sql.SQLException
import scala.reflect.Manifest
import com.mysql.jdbc.exceptions.MySQLTransientException
import com.twitter.querulous.database.{PoolTimeoutException, PoolEmptyException, SqlDatabaseTimeoutException}
import com.twitter.querulous.query.SqlQueryTimeoutException
import com.twitter.querulous.evaluator.{QueryEvaluator, QueryEvaluatorProxy}
import com.twitter.gizzard.shards._
class SqlExceptionWrappingProxy(shardId: ShardId) extends ExceptionHandlingProxy({e =>
e match {
case e: SqlQueryTimeoutException =>
throw new ShardTimeoutException(e.timeout, shardId, e)
case e: SqlDatabaseTimeoutException =>
throw new ShardDatabaseTimeoutException(e.timeout, shardId, e)
case e: PoolTimeoutException =>
throw new NormalShardException(e.toString, shardId, e)
case e: PoolEmptyException =>
throw new NormalShardException(e.toString, shardId, e)
case e: MySQLTransientException =>
throw new NormalShardException(e.toString, shardId, null)
case e: SQLException =>
if ((e.toString contains "Connection") && (e.toString contains " is closed")) {
throw new NormalShardException(e.toString, shardId, null)
} else {
throw new ShardException(e.toString, e)
}
case e: ShardException =>
throw e
}
})
class SqlExceptionWrappingProxyFactory[T <: AnyRef : Manifest](id: ShardId) extends ExceptionHandlingProxyFactory[T]({ (shard, e) =>
val manifest = implicitly[Manifest[T]]
e match {
case e: SqlQueryTimeoutException =>
throw new ShardTimeoutException(e.timeout, id, e)
case e: SqlDatabaseTimeoutException =>
throw new ShardDatabaseTimeoutException(e.timeout, id, e)
case e: PoolTimeoutException =>
throw new NormalShardException(e.toString, id, e)
case e: PoolEmptyException =>
throw new NormalShardException(e.toString, id, e)
case e: MySQLTransientException =>
throw new NormalShardException(e.toString, id, null)
case e: SQLException =>
if ((e.toString contains "Connection") && (e.toString contains " is closed")) {
throw new NormalShardException(e.toString, id, null)
} else {
throw new ShardException(e.toString, e)
}
case e: ShardException =>
throw e
}
})
class ShardExceptionWrappingQueryEvaluator(shardId: ShardId, evaluator: QueryEvaluator) extends QueryEvaluatorProxy(evaluator) {
override protected def delegate[A](f: => A) = {
try {
f
} catch {
case e: SqlQueryTimeoutException =>
throw new ShardTimeoutException(e.timeout, shardId, e)
case e: SqlDatabaseTimeoutException =>
throw new ShardDatabaseTimeoutException(e.timeout, shardId, e)
case e: PoolTimeoutException =>
throw new NormalShardException(e.toString, shardId, e)
case e: PoolEmptyException =>
throw new NormalShardException(e.toString, shardId, e)
case e: MySQLTransientException =>
throw new NormalShardException(e.toString, shardId, null)
case e: SQLException =>
if ((e.toString contains "Connection") && (e.toString contains " is closed")) {
throw new NormalShardException(e.toString, shardId, null)
} else {
throw new ShardException(e.toString, e)
}
case e: ShardException =>
throw e
}
}
}