0

Here is a part from my program:

try {
        Class.forName("com.mysql.jdbc.Driver");
        InitializeData data = new InitializeData();

        Connection con = null;
        try {
            Context ctx = data.getContext();
            MysqlDataSource ds = (MysqlDataSource)ctx.lookup("database");
            con = ds.getConnection();
       ...

where

public InitializeData() {

    // configuring data source (data base)
    ds = new MysqlDataSource();
    ds.setUser("root");
    ds.setPassword("%");
    ds.setServerName("localhost");
    ds.setPort(3306);

    // configuring jndi
    try {
        Properties env = new Properties();

        try {
            env.load(new FileInputStream(env_props));
        } catch (FileNotFoundException e) {
            System.err.println("Unable to load jndi properties");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("IOException");
            System.exit(1);
        }

        ctx = new InitialContext(env);
        ctx.rebind("database", ds);

    } catch (NamingException e) {
        System.err.println("Naming Exception");
        System.exit(1);
    } 


}

public Context getContext() {
    return ctx;
}

As you can see, I try to connect to MySQL. Actually, MySQL is embedded in my java program. When I connect via DriverManager (using con = DriverManager.getConnection("jdbc:mysql:mxj://localhost", "root", "");) everything works. But using a DataSource object as it shown above invokes an exception:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:425)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:140)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:110)
at statistics.DatabaseWorks.main(DatabaseWorks.java:26)

Could anybody, please, help me?


Some tests viewed that my program attempts to connect to a MySQL server, which is not embedded (see BalusC's answer). If I start the server(not embedded) everything works. So I need a way to say my program to start the local one. For some reason ds.setUrl("jdbc:mysql:mxj://"); doesn't work

Dmitry
  • 3,028
  • 6
  • 44
  • 66

1 Answers1

1

You're attempting to connect to a real MySQL server instead of an embedded MySQL server.

I've never tried it, but the following should in theory work:

ds.setUrl("jdbc:mysql:mxj://localhost");
ds.setUser("root");
ds.setPassword("");

So with exactly the same parameters as in your DriverManager#getConnection().

By the way, the InitializeData thing is pretty overcomplicated. I'd suggest to simplify this. Since you are creating the datasource manually, you don't need to put it in JNDI scope at all. Just create ds and use ds.getConnection() directly. The JNDI is only useful if you have delegated the creation of the datasource to some JNDI container like a Java EE application server.


Update as per the comments: it just look like that MysqlDataSource isn't designed for embedded MySQL at all. If all you want is a connection pooled datasource, try c3p0. It should work universally since all it needs is the full JDBC URL, username and password, exactly the same as you pass to DriverManager.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have tried this but these changes provided the connection-refused-exception again. – Dmitry Dec 11 '10 at 17:57
  • Well, I think (agreed with you =) ) that I'm trying to connect to a MySQL server which is not embedded. How can I make my program to use the embedded one? – Dmitry Dec 11 '10 at 18:04
  • That is TRUE! I have started MySQL (not embedded) and everything works!!! SO that it doesn't understand that it needs to start a local server – Dmitry Dec 11 '10 at 18:06
  • If you're using a real MySQL server, you may find this [mini tutorial](http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql/2840358#2840358) useful as well. – BalusC Dec 11 '10 at 18:50
  • Actually, I must use an embedded server – Dmitry Dec 11 '10 at 19:02