Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm developing small Java Web Applications on Tomcat server and I'm using MySQL as my database.

Up until now I was using a connection singleton for accessing the database but I found out that this will ensure just on connection per Application and there will be problems if multiple users want to access the database in the same time. (They all have to make us of that single Connection object). I created a Connection Pool and I hope that this is the correct way of doing things.

Furthermore it seems that I developed the bad habit of creating a lot of static object and static methods (mainly because I was under the wrong impression that every static object will be duplicated for every client which accesses my application).

Because of this all the Service Classes ( classes used to handle database data) are static and distributed through a ServiceFactory:

public class ServiceFactory {

private static final String JDBC = "JDBC";
private static String impl;

private static AccountService accountService;
private static BoardService boardService;

public static AccountService getAccountService(){
    initConfig();

    if (accountService == null){
        if (impl.equalsIgnoreCase(JDBC)){
            accountService = new JDBCAccountService();
        }
    }

    return accountService;
}

public static BoardService getBoardService(){
     initConfig();

    if (boardService == null){
        if (impl.equalsIgnoreCase(JDBC)){
            boardService = new JDBCBoardService();
        }
    }

    return boardService;
}
private static void initConfig(){
    if (StringUtil.isEmpty(impl)){
        impl = ConfigUtil.getProperty("service.implementation");

        // If the config failed initialize with standard
        if (StringUtil.isEmpty(impl)){
            impl = JDBC;
        }
    }
}

This was the factory class which, as you can see, allows just one Service to exist at any time. Now, is this a bad practice? What happens if let's say 1k users access AccountService simultaneously?

I know that all this questions and bad practices come from a bad understanding of the static attribute in a web application and the way the server handles this attributes. Any help on this topic would be more than welcomed. Thank you for your time!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Those are basically solved problems. Just create a new connection every time you need to access the database (or maybe just once per request, if that's easily possible), and use a database API that supports pooled connections. Just make sure you close all connections no matter what, but that's something you should be doing anyway. No need to reinvent the wheel here.

And may I note that making the database connection a singleton is a bad design decision in the first place, because of the exact problem you've run into. There is absolutely no reason to enforce that there is only ever one connection to the database from your application.

share|improve this answer
    
Thank you! I know that I can use JPA or Hibernate and I will not run in this kind of problems but the aim of this applications is to learn the basics as well. Are you saying that static Service methods are a bad practice as well? Thank you for your help! –  Ionut May 5 '12 at 11:32
    
Personally, I don't see why they would have to be static. If you use pooled connections, the overhead of creating new instances should be minimal, and the DBMS itself mostly takes care of concurrency issues. –  tdammers May 5 '12 at 11:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.