Index: trac/db.py
===================================================================
--- trac/db.py	(revision 2342)
+++ trac/db.py	(working copy)
@@ -183,7 +183,10 @@
                     del self._active[tid]
                     if cnx not in self._dormant:
                         cnx.rollback()
-                        self._dormant.append(cnx)
+                        if cnx.poolable:
+                            self._dormant.append(cnx)
+                        else:
+                            self._cursize -= 1
                         self._available.notify()
         finally:
             self._available.release()
@@ -238,6 +241,8 @@
 
     __slots__ = ['cnx']
 
+    poolable = False
+
     def __init__(self, path, params={}):
         assert have_pysqlite > 0
         self.cnx = None
@@ -262,7 +267,7 @@
             sqlite.register_converter('TEXT', str)
 
             cnx = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES,
-                                 check_same_thread=False, timeout=timeout)
+                                 timeout=timeout)
         else:
             cnx = sqlite.connect(path, timeout=timeout)
         ConnectionWrapper.__init__(self, cnx)
@@ -332,6 +337,8 @@
 
     __slots__ = ['cnx']
 
+    poolable = True
+
     def __init__(self, path, user=None, password=None, host=None, port=None,
                  params={}):
         if path.startswith('/'):

