开发者

MysqlDataSource Invokes "Connection refused" Exception

开发者 https://www.devze.com 2023-01-29 21:13 出处:网络
Here is a part from my program: try { Class.forName(\"com.mysql.jdbc.Driver\"); InitializeData data = new InitializeData();

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");
            Syst开发者_如何学Goem.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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号