repoapi.py
changeset 9060 8c0016d7a091
parent 9058 2f45e99ad753
child 9061 e86fdab3d296
equal deleted inserted replaced
9059:795ea498225f 9060:8c0016d7a091
    92     """A Connection object to be used Client side.
    92     """A Connection object to be used Client side.
    93 
    93 
    94     This object is aimed to be used client side (so potential communication
    94     This object is aimed to be used client side (so potential communication
    95     with the repo through RTC) and aims to offer some compatibility with the
    95     with the repo through RTC) and aims to offer some compatibility with the
    96     cubicweb.dbapi.Connection interface.
    96     cubicweb.dbapi.Connection interface.
       
    97 
       
    98     The autoclose_session paramenter informs the connection that this session
       
    99     have been open explictly and only for this client connection. The
       
   100     connection will close the session of exit.
    97     """
   101     """
    98     # make exceptions available through the connection object
   102     # make exceptions available through the connection object
    99     ProgrammingError = ProgrammingError
   103     ProgrammingError = ProgrammingError
   100     # attributes that may be overriden per connection instance
   104     # attributes that may be overriden per connection instance
   101     anonymous_connection = False # XXX really needed ?
   105     anonymous_connection = False # XXX really needed ?
   102     is_repo_in_memory = True # BC, always true
   106     is_repo_in_memory = True # BC, always true
   103 
   107 
   104     def __init__(self, session):
   108     def __init__(self, session, autoclose_session=False):
   105         self._session = session
   109         self._session = session
   106         self._cnxid = None
   110         self._cnxid = None
   107         self._open = None
   111         self._open = None
   108         self._web_request = False
   112         self._web_request = False
   109         self.vreg = session.vreg
   113         self.vreg = session.vreg
   110         self._set_user(session.user)
   114         self._set_user(session.user)
       
   115         self._autoclose_session = autoclose_session
   111 
   116 
   112     def __enter__(self):
   117     def __enter__(self):
   113         assert self._open is None
   118         assert self._open is None
   114         self._open = True
   119         self._open = True
   115         self._cnxid = '%s-%s' % (self._session.id, uuid4().hex)
   120         self._cnxid = '%s-%s' % (self._session.id, uuid4().hex)
   120         self._open = False
   125         self._open = False
   121         cnxid = self._cnxid
   126         cnxid = self._cnxid
   122         self._cnxid = None
   127         self._cnxid = None
   123         self._session._cnx.ctx_count -= 1
   128         self._session._cnx.ctx_count -= 1
   124         self._session.close_cnx(cnxid)
   129         self._session.close_cnx(cnxid)
       
   130         if self._autoclose_session:
       
   131             # we have to call repo.close to unsure the repo properly forget the
       
   132             # session calling session.close() is not enought :-(
       
   133             self._session.repo.close(self._session.id)
   125 
   134 
   126 
   135 
   127     # begin silly BC
   136     # begin silly BC
   128     @property
   137     @property
   129     def _closed(self):
   138     def _closed(self):