0

We used a special database,so i want to write a database connection Pool,there is something wrong with the code, when i debug into the static block,at first JVM worked in the Properties properties = new Properties(); but next step is int i = 0; i dont know why it ingored the other code in the static block

public class GoldDataSource {

private static String goldip;
private static int goldport;
private static String username;
private static String password;
private static int maxPollSize;
private static LinkedList<Server> list = new LinkedList<Server>();

private static Logger logger = LoggerFactory.getLogger(GoldDataSource.class);

static {
    try {
        Properties properties = new Properties();
        InputStream is = GoldDataSource.class.getClassLoader().getResourceAsStream("/conf/db/gold.properties");
        properties.load(is);
        goldip = properties.getProperty("gold.ip");
        goldport = Integer.parseInt(properties.getProperty("gold.port"));
        username = properties.getProperty("gold.username");
        password = properties.getProperty("gold.password");
        maxPollSize = Integer.parseInt(properties.getProperty("gold.pool.maxPollSize"));
        for (int i = 0; i < maxPollSize; i++) {
            Server server = new ServerImpl(goldip, goldport, username, password);
            server.connect();
            server.login();
            list.add(server);
        }
    } catch (Exception e) {
        ....
    }
}


public synchronized static Server getServer() {
    try {
        int i = 0;
        while (true) {
            Server aServer = list.removeFirst();
            if (aServer != null) {
                return aServer;
            } else {
                if (i >= 3) {

                    return aServer;
                }
                Thread.sleep(500);
                i++;
            }
        }
    } catch (Exception e) {
        ...
        return null;
    }
}

public static void closeServer(Server aServer) {
    list.addLast(aServer);
}}

I used Server server = GoldDataSource.getServer(); to get connection,but when it executed Properties properties = new Properties();then jump into the code int i = 0; which in the getServer() method


I edited the code with singleton pattern,it worked ,but i still do not understand why the old code didn't worked , and i catched "java.lang.reflect.InvocationTargetException" in the first catch block old code.

public class GoldDataSource {

private static String goldip;
private static int goldport;
private static String username;
private static String password;
private static int maxPollSize;
private static LinkedList<Server> list = new LinkedList<Server>();
private static InputStream is;

private static GoldDataSource aDataSource = new GoldDataSource();

private static Logger logger = LoggerFactory.getLogger(GoldDataSource.class);


private goldDataSource() {
    try {
        Properties properties = new Properties();
        is = GoldDataSource.class.getClassLoader().getResourceAsStream("/conf/db/gold.properties");
        properties.load(is);
        goldip = properties.getProperty("gold.ip");
        goldport = Integer.parseInt(properties.getProperty("gold.port"));
        username = properties.getProperty("gold.username");
        password = properties.getProperty("gold.password");
        maxPollSize = Integer.parseInt(properties.getProperty("gold.pool.maxPollSize"));
        for (int i = 0; i < maxPollSize; i++) {
            Server server = new ServerImpl(goldip, goldport, username, password);
            server.connect();
            server.login();
            list.add(server);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static goldDataSource getDateSource() {
    return aDataSource;
}


public synchronized Server getServer() {
    try {
        int i = 0;
        while (true) {
            Server aServer = list.removeFirst();
            if (aServer != null) {
                return aServer;
            } else {
                if (i >= 3) {
                    return aServer;
                }
                Thread.sleep(500);
                i++;
            }
        }
    } catch (Exception e) {
        logger.error("get connection error:" + e.toString());
        return null;
    }
}

public void closeServer(Server aServer) {
    list.addLast(aServer);
}

}

  • Where is `Server server = GoldDataSource.getServer();` line in the code? – Darshan Mehta Apr 13 '17 at 09:37
  • 2
    Perhaps `getResourceAsStream` fails? – Simon Apr 13 '17 at 09:38
  • Are you talking about breakpoints in your IDE? – Steve Smith Apr 13 '17 at 09:40
  • 1) Remember to close the `InputStream`. 2) You should not do all that in a `static` block. Instead, you should implement this using the singleton pattern, e.g. see [What is an efficient way to implement a singleton pattern in Java?](http://stackoverflow.com/q/70689/5221149) – Andreas Apr 13 '17 at 09:41
  • @Steve Smith yes in the IDE – pingandpong Apr 13 '17 at 09:47
  • For future posts, please take the time to read your own question, and fix any mistakes you may find. It is hard to help if one also has to deal with broken language, typos, wrong formatting, etc. – C-Otto Apr 13 '17 at 09:50
  • 1
    What is in your `catch` block in your static initializer? It seems most likely that the intializer is failing and you are swallowing the exception. – DaveH Apr 13 '17 at 09:57
  • @DaveH just print the log – pingandpong Apr 13 '17 at 10:46
  • @C-Otto THX,sorry about that,i am not good at "markdown" skill – pingandpong Apr 13 '17 at 10:48
  • @Andreas yes i forgot about it,but i dont konw why it didn't worked – pingandpong Apr 13 '17 at 10:50
  • @pingandpong the formatting is easy to fix, but typos and confusing sentences are not. – C-Otto Apr 13 '17 at 10:55
  • @C-Otto thanks for your advice, my mother language is not english but i will pay more attention on it – pingandpong Apr 13 '17 at 11:02
  • So does anything get printed by your catch block? – DaveH Apr 13 '17 at 11:29
  • @DaveH no,I'm at home now. I can't connect to the database. I'll try again tomorrow – pingandpong Apr 13 '17 at 12:00
  • Static _initializer_ blocks are to initialize a class. Yours does way too much for logic that executes before a class is initialized. Get rid of all the crap from the initializer that has nothing to do with class initialization, and likely shouldn't even be static in the first place. You're begging for bugs the way you're doing it. And remember, it's "static _initializer_ block", for a reason. – Lew Bloch Apr 13 '17 at 14:05
  • @LewBlochI know that this writing may not appropriate, I loaded the properties in the static block, but I still do not understand the problem, I hope to know why. – pingandpong Apr 13 '17 at 14:33

0 Answers0