JDBC connection to Virtuoso under LOG4JDBC and DBCP

Submitted by admin on Wed, 02/24/2016 - 12:25

I started to use Apache DBCP because I stumbled upon this error when I was running thousands of SQL queries.

virtuoso.jdbc4.VirtuosoException: Connection failed: Cannot assign requested address

THE SOLUTION

For initial settings, please check this previous post here

DBCP (Database Connection Pools) is described here

http://commons.apache.org/proper/commons-dbcp/

I used the library commons-dbcp2 recommended for my Java version

java version "1.7.0_72"
Java(TM) SE Runtime Environment (build 1.7.0_72-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.72-b04, mixed mode)

Next, I changed the class Database described in my previous post in this way

a) The simple way

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
 
public final class Database {
 
    private static final BasicDataSource dataSource = new BasicDataSource();
 
    static {
        dataSource.setDriverClassName("virtuoso.jdbc4.Driver");
        dataSource.setUrl("jdbc:virtuoso://localhost:1111/");   
        dataSource.setUsername("dba");
        dataSource.setPassword("dba");
    }
 
    private Database() {
        //
    }
 
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
 
}

b) The way using LOG4JDBC to log SQL queries

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
 
public final class Database {
 
    private static final BasicDataSource dataSource = new BasicDataSource();
 
    static {
        dataSource.setDriverClassName("net.sf.log4jdbc.DriverSpy");        
        dataSource.setUrl("jdbc:log4jdbc:virtuoso://localhost:1112/");        
        dataSource.setUsername("dba");
        dataSource.setPassword("dba");
    }
 
    private Database() {
        //
    }
 
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
 
}

I drop my pom.xml here

<project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>" xsi:schemaLocation="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a> <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd">http://maven.apache.org/xsd/maven-4.0.0.xsd</a>">
  <modelVersion>4.0.0</modelVersion>
  <groupId>virtuoso</groupId>
  <artifactId>virtuoso</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source/>
          <target/>
        </configuration>
      </plugin>
    </plugins>
  </build>
    <dependencies>
<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>apache-jena-libs</artifactId>
    <type>pom</type>
    <version>2.13.0</version>
    <exclusions>
      <exclusion>
        <artifactId>slf4j-log4j12</artifactId>
        <groupId>org.slf4j</groupId>
      </exclusion>
    </exclusions>
</dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-collections4</artifactId>
          <version>4.0</version>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.3.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-math3</artifactId>
          <version>3.4.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-csv</artifactId>
          <version>1.1</version>
        </dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.16</version>
</dependency>
    	<dependency>
    		<groupId>org.apache.commons</groupId>
    		<artifactId>commons-dbcp2</artifactId>
    		<version>2.1.1</version>
    	</dependency>
    	<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>
            <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
 
    </dependencies>
</project>

References

http://stackoverflow.com/questions/7592056/am-i-using-jdbc-connection-p…