cubicweb #1534590 Keep references on cnx to avoid closing sessions [deprecated]

In a little script, I've used the following code:

def get_repo_session():
  from cubicweb.dbapi import in_memory_cnx
  from cubicweb.etwist.twconfig import AllInOneConfiguration
  config = AllInOneConfiguration('mycube')
  repo, cnx = in_memory_cnx(config, login='xxx', password='xxx')
  session = repo._get_session(cnx.sessionid)
  return session

session = get_repo_session()
# and so on

and have been bitten by an AttributeError : Session has no _tx_data attribute.

The reason here is that cnx (from get_repo_session() no longer being referenced, cnx.__del__ is called and session is closed.

One easy solution would be for session to keep a reference to corresponding cnx.

done in<not specified>
load left0.000
closed by<not specified>