protected void checkLeaks() {
   int finalOpenSessions = CoreInstance.getInstance().getNumberOfSessions();
   int leakedOpenSessions = finalOpenSessions - initialOpenSessions;
   if (leakedOpenSessions != 0) {
     log.error(
         String.format(
             "There are %s open session(s) at tear down; it seems "
                 + "the test leaked %s session(s).",
             Integer.valueOf(finalOpenSessions), Integer.valueOf(leakedOpenSessions)));
     for (CoreInstance.RegistrationInfo info : CoreInstance.getInstance().getRegistrationInfos()) {
       log.warn("Leaking session", info);
     }
   }
   int finalSingleConnections = ConnectionHelper.countConnectionReferences();
   int leakedSingleConnections = finalSingleConnections - initialSingleConnections;
   if (leakedSingleConnections > 0) {
     log.error(
         String.format(
             "There are %s single datasource connection(s) open at tear down; "
                 + "the test leaked %s connection(s).",
             Integer.valueOf(finalSingleConnections), Integer.valueOf(leakedSingleConnections)));
   }
   ConnectionHelper.clearConnectionReferences();
 }
 protected void initCheckLeaks() {
   initialOpenSessions = CoreInstance.getInstance().getNumberOfSessions();
   initialSingleConnections = ConnectionHelper.countConnectionReferences();
 }
Example #3
0
 /**
  * Gets the Jena graph using options.
  *
  * <p>The Jena "Convenient" reification style is used when opening models: it allows to ignore
  * reification quadlets when calling the statements list.
  *
  * @param forceReload boolean stating if the jena graph has to be reloaded using options
  * @return the Jena graph (model)
  */
 protected synchronized GraphConnection openGraph(boolean forceReload) {
   // create model given backend
   if (backend.equals("memory")) {
     if (memoryGraph == null || forceReload) {
       memoryGraph = ModelFactory.createDefaultModel(ModelFactory.Convenient);
       memoryGraph.setNsPrefixes(namespaces);
     }
     return new GraphConnection((Connection) null, memoryGraph);
   } else if (backend.equals("sql")) {
     if (datasource == null) {
       throw new IllegalArgumentException("Missing datasource for sql graph : " + name);
     }
     // create a database connection
     Connection baseConnection;
     try {
       // try single-datasource non-XA mode
       baseConnection = ConnectionHelper.getConnection(datasource);
       if (baseConnection == null) {
         // standard datasource usage
         DataSource ds = DataSourceHelper.getDataSource(datasource);
         baseConnection = ds.getConnection();
       }
     } catch (NamingException e) {
       throw new IllegalArgumentException(String.format("Datasource %s not found", datasource), e);
     } catch (SQLException e) {
       throw new IllegalArgumentException(
           String.format("SQLException while opening %s", datasource), e);
     }
     /*
      * We have to wrap the connection to disallow any commit() or setAutoCommit() on it. Jena calls these
      * methods without regard to the fact that the connection may be managed by an external transaction.
      */
     Connection wrappedConnection =
         (Connection)
             Proxy.newProxyInstance(
                 Connection.class.getClassLoader(),
                 new Class[] {Connection.class},
                 new ConnectionFixInvocationHandler(baseConnection));
     DBConnection connection = new DBConnection(wrappedConnection, databaseType);
     // check if named model already exists
     Model graph;
     if (connection.containsModel(name)) {
       ModelMaker m = ModelFactory.createModelRDBMaker(connection, ModelFactory.Convenient);
       graph = m.openModel(name);
     } else {
       // create it
       // check if other models already exist for that connection.
       if (connection.getAllModelNames().hasNext()) {
         // other models already exist => do not set parameters
         // on driver.
         if (databaseDoCompressUri != connection.getDriver().getDoCompressURI()) {
           log.warn(
               String.format(
                   "Cannot set databaseDoCompressUri attribute to %s "
                       + "for model %s, other models already "
                       + "exist with value %s",
                   databaseDoCompressUri, name, connection.getDriver().getDoCompressURI()));
         }
         if (databaseTransactionEnabled != connection.getDriver().getIsTransactionDb()) {
           log.warn(
               String.format(
                   "Cannot set databaseTransactionEnabled attribute to %s "
                       + "for model %s, other models already "
                       + "exist with value %s",
                   databaseTransactionEnabled, name, connection.getDriver().getIsTransactionDb()));
         }
       } else {
         if (databaseDoCompressUri) {
           connection.getDriver().setDoCompressURI(true);
         }
         if (databaseTransactionEnabled) {
           connection.getDriver().setIsTransactionDb(true);
         }
       }
       ModelMaker m = ModelFactory.createModelRDBMaker(connection, ModelFactory.Convenient);
       graph = m.createModel(name);
     }
     graph.setNsPrefixes(namespaces);
     // use baseConnection so that it is closed instead of the jena one
     // (to let the pool handled closure).
     if (baseConnection != null) {
       return new GraphConnection(baseConnection, graph);
     }
     return new GraphConnection(connection, graph);
   } else {
     throw new IllegalArgumentException("Unknown backend type " + backend);
   }
 }