Esempio n. 1
0
 protected void close() {
   if (connection != null) {
     try {
       connection.close();
     } catch (SQLException e) {
       log.error("Could not close connection");
     }
   }
   if (baseConnection != null) {
     try {
       baseConnection.close();
     } catch (SQLException e) {
       log.error("Could not close base connection");
     }
   }
 }
Esempio n. 2
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);
   }
 }