2

Alrighty,

I'm trying to connect to a few different databases using Apache connection pool, but I'm having difficulty with LocalDb via Jtds. I get the following error message:

Aug 31, 2018 8:49:24 PM Model.DataAccessLayer.Database.ConnectionPool_LocalDb main
SEVERE: null
java.sql.SQLException: Network error IOException: \\.\pipe\MSSQL$LOCALDB#F1954C00\sql\query (The system cannot find the file specified)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at org.apache.commons.dbcp2.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:85)
    at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:260)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
    at org.apache.commons.dbcp2.PoolingDriver.connect(PoolingDriver.java:128)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at Model.DataAccessLayer.Database.ConnectionPool_LocalDb.getConnection(ConnectionPool_LocalDb.java:175)
    at Model.DataAccessLayer.Database.ConnectionPool_LocalDb.main(ConnectionPool_LocalDb.java:100)
Caused by: java.io.FileNotFoundException: \\.\pipe\MSSQL$LOCALDB#F1954C00\sql\query (The system cannot find the file specified)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:124)
    at net.sourceforge.jtds.jdbc.SharedLocalNamedPipe.<init>(SharedLocalNamedPipe.java:64)
    at net.sourceforge.jtds.jdbc.JtdsConnection.createNamedPipe(JtdsConnection.java:518)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:328)

The code I'm playing about with looks this. As you can see I've tried a few different connection strings as my gut feeling this is where the issue lay. I've :

    public static void main(String[] args) {

    //Connection Pool
    ConnectionPool_LocalDb pool = null;
    String driverPoolName = "docs";

    try {
        //Initialise H2 Connection
        String dbConnStrH2 = "jdbc:h2:~/test";
        String sqlH2 = "SELECT * FROM TEST2;";
        //Initialise Connection Pool
        pool = new ConnectionPool_LocalDb("jdbc:apache:commons:dbcp:", "org.apache.commons.dbcp2.PoolingDriver");
        //H2 Database
        pool.setupDriver(dbConnStrH2, driverPoolName);
        Connection h2Connection = pool.getConnection(driverPoolName);
        ResultSet h2ResultSet = pool.getResultSet(h2Connection, sqlH2);
        System.out.println("h2ResultSet = " + pool.getResultSetAsString(h2ResultSet));
        System.out.println("poolStats = " + pool.getPrintPoolStats(driverPoolName));

        //Initialise LocalDb Connection
        String driverPoolNameLocalDb = "localDb";
//        String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./instance=LOCALDB#F1954C00;namedPipe=true";
//        String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./instance=LOCALDB#F1954C00;namedPipe=true";
//        String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./;instance=LOCALDB#F1954C00;namedPipe=true";
//        String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./docs;instance=LOCALDB#F1954C00;namedPipe=true;domain=WORKGROUP";
          String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./;instance=LOCALDB#F1954C00;namedPipe=true;domain=WORKGROUP";

        String sqlLocalDb = "SELECT * FROM [docs].[dbo].[documents]";
        //Sql Server LocalDb
        pool.setupDriver(dbConnStrLocalDb, driverPoolName);
        Connection LocalDbConnection = pool.getConnection(driverPoolName);
        ResultSet LocalDbResultSet = pool.getResultSet(LocalDbConnection, sqlLocalDb);
        System.out.println("LocalDbResultSet = " + pool.getResultSetAsString(LocalDbResultSet));
        System.out.println("poolStats = " + pool.getPrintPoolStats(driverPoolNameLocalDb));

        //Shutting down connections
        h2Connection.close();
        LocalDbConnection.close();
        pool.shutdownDriver(driverPoolName);

    } catch (Exception ex) {
        Logger.getLogger(ConnectionPool_LocalDb.class.getName()).log(Level.SEVERE, null, ex);
    }
    //Shut down driver pools
    try {
        pool.shutdownDriver(driverPoolName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Perhaps I might be suffering from the same issue as Ajeh on:- Connecting to SQL Server LocalDB using JDBC

The error is occurring on the line Connection LocalDbConnection = pool.getConnection(driverPoolName);

Annoyingly its really easy to connect to the production SQL server through jtds but sadly I can't do this for development phase.

I've managed to connect to the LocalDb via Sql Server Management Studio.

Any assistance would be greatly appreciated.

OutsideCoder
  • 346
  • 3
  • 16
  • It looks like you are successfully starting with DBCP2 using JTDS but JTDS cant find your named pipe, can you confirm the existence of the pipe? – xtratic Aug 31 '18 at 20:19
  • Yip I meant to post that apologies: Name: Shhh Version: 13.1.4001.0 Shared name: Owner: DESKTOP-LAP\MyName Auto-create: No State: Running Last start time: 01/09/2018 00:11:47 Instance pipe name: np:\\.\pipe\LOCALDB#F1954C00\tsql\query – OutsideCoder Aug 31 '18 at 23:14

0 Answers0