1

So I'm making a server application in java, and I want the database requests to be event driven, so that it's non-blocking.

The way I'm doing this is to create the MySQL connection in another thread, so far I've got this:

package makeza.server.persistence;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.logging.Logger;

import makeza.server.WorldServer;

public final class MySQLNonblock extends Thread {
    public Connection connection = null;
    private Logger log = WorldServer.log;

    private String host, port, user, pass, database;

    @Override
    public void run(){
        init();
    }

    public MySQLNonblock(String hst, String prt, String usr, String pas, String dbase){
        this.setDaemon(true);
        this.setName("DatabaseThread");
        this.host = hst;
        this.port = prt;
        this.user = usr;
        this.pass = pas;
        this.database = dbase;
    }

    public void init(){
        log.info("Connecting to database...");
        try {
            connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + 
                                        database + "?" + "user=" + user + "&password=" + pass);
            log.info("Connected to database");
        } catch (SQLException e) {
            log.severe("Couldn't connect to database at " + host + ":" + port);
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
            System.exit(1);
            //e.printStackTrace();
        }
    }

}

I start the thread in another class, and it connects to the mysql server, and then the thread exits.

How do I make the thread persist once then run method ends so that I can use the mysql connection?

1
  • You want a message pump on that thread? Commented Nov 3, 2011 at 16:28

2 Answers 2

2

You want to use connection pooling.

"Closing" a connection in this respect means "giving a connection back to the pool for reuse".

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, given that it should be the "business/ application logic" that drives activity. That's where the threading & primary focus of app design should be, the DB connection should be largely passive & subordinate, and come from a ConnectionPool.
0

You need to wait until some other thread wakes it by notifying it. Read the tutorial about concurrency: http://download.oracle.com/javase/tutorial/essential/concurrency/

You should probably use a more high-level abstraction though, like an ExecutorService.

3 Comments

Code example? Just a short one?
See download.oracle.com/javase/tutorial/essential/concurrency/… and download.oracle.com/javase/tutorial/essential/concurrency/…. But if you're serious about multi-threading, you'd better learn how all this works in details rather than simply copy'n pasting some code examples.
Wrong design! Better to multi-thread the Request Processor/ actual activity. This can then grab/ and return connections to a largely-passive Connection Pool. (Connection pools are a standard thing, you don't need to write one yourself.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.