Results 1 to 5 of 5
  1. #1
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Problems returning a database connection

    I going through one of my apps trying to make it use different threads to improve the responsiveness and I have a logic problem that I can't seem to get passed.

    I have a class called sqlconnection that does nothing more than spin up a database connection and returns the Connection. I have a second class called VersionTable that creates one table

    Right now the code looks like this and it works fine:
    Java Code:
        Sqlconnection connect = new Sqlconnection();
        con = connect.serverconnection(username, pass, domain, server);
            if(con!=null){
              VersionTable schema_version = new VersionTable();
              schema_version.createtable(database, version, con);
            }
    I want to make this multi-threaded though. This is what I have so far.

    Java Code:
            Thread runConnection = new Thread(new sqlconnection(username, pass, domain, server));
            runConnection.start();
            
            if(con!=null){
              Thread runVersiontable = new Thread(new VersionTable(database, version, con));
              runVersiontable.start();
            }
    The problem is that I don't know how to return the value of my Connection con to a thread? Without that, my if statement fails.

    Also, how do I tell my second thread not to start until the first one completes?

    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problems returning a database connection

    What sort of application is this?
    If it's a desktop (ie single user) one then why the need to have the connection builder in a separate thread? It gains you nothing as the following piece of code cannot run without a connection.

    If it's a multi user (eg server) environment then you should be using a connection pool, and so not worrying about the connections.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Problems returning a database connection

    Its a single user GUI (Swing) that creates tables in a SQL Server database. The reason I was thinking of putting the connection in a separate thread is because sometimes our network is slammed and the connection can take a few seconds. While that is going on, my GUI is frozen. I guess its just an annoyance more than anything.

    Plus, being new to Java I didn't know how to pass a connection back to Main and I didn't know how to synchronize two threads, so I thought it would be a good exercise to run through.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problems returning a database connection

    Any task that requires interacting with non-gui stuff should be done on a thread.
    That's what SwingWorkers are for.
    So the whole task which the above code is a part should be done using a SwingWorker (which are threads).

    You don't break the individual task down and stick them all in threads...you do the whole thing.

    Look up MVC.
    You have your View.
    Your Controller does the work of finding/manipulating/sending data.
    The Model represents your data.

    The stuff done by the Controller should be launched via workers (assuming it's not some general background task).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Problems returning a database connection

    I found the solution. I just had to use Callable and an executor service instead of the Runnable interface.

Similar Threads

  1. 3G Connection problems
    By sumerfutt in forum New To Java
    Replies: 6
    Last Post: 05-29-2013, 04:50 PM
  2. Replies: 1
    Last Post: 06-06-2011, 01:47 PM
  3. Problems about connection to a database
    By berlindutza in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-06-2010, 10:21 PM
  4. Replies: 0
    Last Post: 03-11-2008, 04:55 PM
  5. Database connection
    By oaklander in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 11:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •