Esempio n. 1
0
    def __init__(self, opts, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        self.opts = opts
        try:
            adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        except dbapi2.DatabaseError as e:
            raise DatabaseAccessError(
                'Error initializing u1db connection pool: %s' % str(e))

        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        try:
            conn = self.connectionFactory(
                self, init_u1db=True)
            replica_uid = conn._u1db._real_replica_uid
            setProxiedObject(self.replica_uid, replica_uid)
        except DatabaseAccessError as e:
            self.threadpool.stop()
            raise DatabaseAccessError(
                "Error initializing connection factory: %s" % str(e))
Esempio n. 2
0
    def connect(self, url='sqlite:///:memory:', engine=None):
        """ Use the database defined by the url or the sqlalchemy engine. """

        assert url or engine

        from zope import proxy
        from sqlalchemy import create_engine, orm

        if url:
            engine = create_engine(url)

        session = orm.scoped_session(orm.sessionmaker(bind=engine))

        if self._session:
            self._session.close()
            proxy.setProxiedObject(self._session, session)
        else:
            self._session = proxy.ProxyBase(session)

        # import models before creating them to ensure that base.metadata
        # contains all tables needed
        from ship import models
        assert models

        self.base.metadata.create_all(bind=engine)
        assert self._session.bind.table_names()
Esempio n. 3
0
    def connect(self, url="sqlite:///:memory:", engine=None):
        """ Use the database defined by the url or the sqlalchemy engine. """

        assert url or engine

        from zope import proxy
        from sqlalchemy import create_engine, orm

        if url:
            engine = create_engine(url)

        session = orm.scoped_session(orm.sessionmaker(bind=engine))

        if self._session:
            self._session.close()
            proxy.setProxiedObject(self._session, session)
        else:
            self._session = proxy.ProxyBase(session)

        # import models before creating them to ensure that base.metadata
        # contains all tables needed
        from ship import models

        assert models

        self.base.metadata.create_all(bind=engine)
        assert self._session.bind.table_names()
Esempio n. 4
0
    def __init__(self, opts, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        self.opts = opts
        try:
            adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        except dbapi2.DatabaseError as e:
            raise DatabaseAccessError(
                'Error initializing u1db connection pool: %s' % str(e))

        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        try:
            conn = self.connectionFactory(
                self, init_u1db=True)
            replica_uid = conn._u1db._real_replica_uid
            setProxiedObject(self.replica_uid, replica_uid)
        except DatabaseAccessError as e:
            self.threadpool.stop()
            raise DatabaseAccessError(
                "Error initializing connection factory: %s" % str(e))
Esempio n. 5
0
    def __init__(self, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        conn = self.connectionFactory(self, init_u1db=True)
        replica_uid = conn._u1db._real_replica_uid
        setProxiedObject(self.replica_uid, replica_uid)
Esempio n. 6
0
    def __init__(self, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        conn = self.connectionFactory(self, init_u1db=True)
        replica_uid = conn._u1db._real_replica_uid
        setProxiedObject(self.replica_uid, replica_uid)
Esempio n. 7
0
    def _runInteraction(self, interaction, *args, **kw):
        """
        Interact with the database and return the result.

        :param interaction:
            A callable object whose first argument is an
            L{adbapi.Transaction}.
        :type interaction: callable
        :return: a Deferred which will fire the return value of
            'interaction(Transaction(...), *args, **kw)', or a Failure.
        :rtype: twisted.internet.defer.Deferred
        """
        tid = self.threadID()
        u1db = self._u1dbconnections.get(tid)
        conn = self.connectionFactory(self,
                                      self._sync_enc_pool,
                                      init_u1db=not bool(u1db))

        if self.replica_uid is None:
            replica_uid = conn._u1db._real_replica_uid
            setProxiedObject(self.replica_uid, replica_uid)

        if u1db is None:
            self._u1dbconnections[tid] = conn._u1db
        else:
            conn._u1db = u1db

        trans = self.transactionFactory(self, conn)
        try:
            result = interaction(trans, *args, **kw)
            trans.close()
            conn.commit()
            return result
        except:
            excType, excValue, excTraceback = sys.exc_info()
            try:
                conn.rollback()
            except:
                logger.error(None, "Rollback failed")
            raise excType, excValue, excTraceback
Esempio n. 8
0
    def _runInteraction(self, interaction, *args, **kw):
        """
        Interact with the database and return the result.

        :param interaction:
            A callable object whose first argument is an
            L{adbapi.Transaction}.
        :type interaction: callable
        :return: a Deferred which will fire the return value of
            'interaction(Transaction(...), *args, **kw)', or a Failure.
        :rtype: twisted.internet.defer.Deferred
        """
        tid = self.threadID()
        u1db = self._u1dbconnections.get(tid)
        conn = self.connectionFactory(
            self, self._sync_enc_pool, init_u1db=not bool(u1db))

        if self.replica_uid is None:
            replica_uid = conn._u1db._real_replica_uid
            setProxiedObject(self.replica_uid, replica_uid)

        if u1db is None:
            self._u1dbconnections[tid] = conn._u1db
        else:
            conn._u1db = u1db

        trans = self.transactionFactory(self, conn)
        try:
            result = interaction(trans, *args, **kw)
            trans.close()
            conn.commit()
            return result
        except:
            excType, excValue, excTraceback = sys.exc_info()
            try:
                conn.rollback()
            except:
                log.err(None, "Rollback failed")
            raise excType, excValue, excTraceback
Esempio n. 9
0
    def __init__(self, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        # extract soledad-specific objects from keyword arguments
        self.opts = kwargs.pop("opts")
        self._sync_enc_pool = kwargs.pop("sync_enc_pool")
        try:
            adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        except DatabaseError:
            raise DatabaseAccessError('Could not open sqlcipher database')

        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        conn = self.connectionFactory(
            self, self._sync_enc_pool, init_u1db=True)
        replica_uid = conn._u1db._real_replica_uid
        setProxiedObject(self.replica_uid, replica_uid)
Esempio n. 10
0
    def __init__(self, *args, **kwargs):
        """
        Initialize the connection pool.
        """
        # extract soledad-specific objects from keyword arguments
        self.opts = kwargs.pop("opts")
        self._sync_enc_pool = kwargs.pop("sync_enc_pool")
        try:
            adbapi.ConnectionPool.__init__(self, *args, **kwargs)
        except DatabaseError:
            raise DatabaseAccessError('Could not open sqlcipher database')

        # all u1db connections, hashed by thread-id
        self._u1dbconnections = {}

        # The replica uid, primed by the connections on init.
        self.replica_uid = ProxyBase(None)

        conn = self.connectionFactory(self,
                                      self._sync_enc_pool,
                                      init_u1db=True)
        replica_uid = conn._u1db._real_replica_uid
        setProxiedObject(self.replica_uid, replica_uid)
Esempio n. 11
0
 def _callFUT(self, *args):
     from zope.proxy import setProxiedObject
     return setProxiedObject(*args)
 def _callFUT(self, *args):
     from zope.proxy import setProxiedObject
     return setProxiedObject(*args)