Index: __init__.py
===================================================================
--- __init__.py	( rev 2025 )
+++ __init__.py	( work copy )
@@ -24,7 +24,7 @@
         (exc, DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
 
 DatabaseError = dbmod.DatabaseError
-db = dbmod.DatabaseWrapper()
+#db = dbmod.DatabaseWrapper()
 dictfetchone = dbmod.dictfetchone
 dictfetchmany = dbmod.dictfetchmany
 dictfetchall = dbmod.dictfetchall
@@ -40,3 +40,118 @@
 OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
 DATA_TYPES = dbmod.DATA_TYPES
 DATA_TYPES_REVERSE = dbmod.DATA_TYPES_REVERSE
+
+
+get_check_sql = dbmod.get_check_sql
+
+#import logging
+#logger = logging.getLogger( 'back' )
+
+class ConnectObjectFactory:
+    def __init__( self ):
+        pass
+        
+    def create_object( self ):
+        return dbmod.DatabaseWrapper()
+    
+    def destroy_object( self , obj ):
+        obj.close()
+        del obj
+    
+    def validate_object( self , obj ):
+        cu = obj.cursor()
+        try:
+            cu.execute( get_check_sql() )
+            return True
+        except:
+            return False
+
+import threading
+import thread
+
+def atomcall( func ):
+    def call( *arg , **kwarg ):
+        try:
+            atomcall.lock.acquire()
+            return func( *arg , **kwarg )
+        finally:
+            atomcall.lock.release()
+    return call
+    
+atomcall.lock = threading.Lock()
+
+class AutoReleaseCursor:
+    def __init__( self , dbwrap , host ):
+        self.dbwrap = dbwrap
+        self.host   = host
+    
+    def __getattr__( self , key ):
+        if hasattr( self.host , key ):
+            return getattr( self.host , key )
+        else:
+            return getattr( self , key )
+    
+    def __del__( self ):
+        #logger.info( 'cursor close %s' , self.dbwrap )
+        self.host.close()
+        self.dbwrap.close()
+    
+import pool
+class DatabaseWrapper:
+    def __init__(self):
+        self.connections = {}
+        self.pool = pool.DBPoolWithThread( ConnectObjectFactory() , freetime = 60 * 5 , threadsafety = dbmod.Database.threadsafety )
+        self.queries = []
+
+    def cursor(self):
+        con = self.getcon()
+        if con is None:
+            con = self.pool.borrow_object()
+            self.setcon( con )
+        if con:
+            return AutoReleaseCursor( self , con.cursor() )
+
+    def commit(self):
+        con = self.getcon()
+        if con:
+            return con.commit()
+
+    def rollback(self):
+        con = self.getcon()
+        if con:
+            return con.rollback()
+
+    def close(self):
+        con = self.getcon()
+        if con is not None:
+            self.pool.return_object( con )
+            self.delcon()
+    
+    def quote_name(self, name):
+        con = self.getcon()
+        if con is None:
+            try:
+                con = self.pool.borrow_object()
+                ret = con.quote_name(name)
+            finally:
+                self.pool.return_object( con )
+        else:
+            ret = con.quote_name(name)
+        return ret
+
+    @atomcall
+    def getcon( self ):
+        threadid = thread.get_ident()
+        return self.connections.get( threadid , None )
+        
+    @atomcall
+    def delcon( self ):
+        threadid = thread.get_ident()
+        del self.connections[ threadid ]
+        
+    @atomcall
+    def setcon( self , con ):
+        threadid = thread.get_ident()
+        self.connections[threadid] = con
+
+db = DatabaseWrapper()
