1

In a very large project where do we set up the database connection so that it is available across all the modules?

Suppose the requirement is like this:

LoginPage.html -> LoginServlet.java -> LoginService.java ==> Takes DB help to check the credentials.

Now, since the actual credentials are stored in DB, where do we set up the database so that the connection is available to all the modules?

In big projects, is database connection made as and when needed or database connections setup at the time when application is run and made available across all the modules.

If DB connections are made available to all the modules (which need DB connectivity), how is this achieved?

Thanks for your help and inputs.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • Create connection pool in server and call the db service as and when needed using lookup. – Anshu Kunal Jun 23 '14 at 07:03
  • @AnshuKunal: Thanks for your inputs, any url / link as to how to do that? – CuriousMind Jun 23 '14 at 07:04
  • @AnshuKunal or use a database connection pool library like c3po or bone cp and configure it to create the connections and retrieve them from there, so there's no need to do any further configuration in the server. – Luiggi Mendoza Jun 23 '14 at 07:06
  • For [connection pooling](http://stackoverflow.com/questions/2835090/jdbc-connection-pooling) – Anshu Kunal Jun 23 '14 at 07:14
  • The answer to this question depends upon your runtime environment. What are you using for a servlet container? What version? Do you use the Spring Framework? CDI? – Steve C Jun 23 '14 at 07:29
  • @SteveC: I was asking in general for web applications which have data stored in DB and there is need to access DB. I was asking for general guideline as to where to keep DB info so that it is available across various layers of the application. – CuriousMind Jun 23 '14 at 08:51

1 Answers1

0

Since you're not using an IoC approach (Spring), the alternative would be to have a static class (or a singleton) that has a reference to the DataSource. Whenever you need a Connection you only have to get it from that class:

public class JdbcUtils{

    private static DataSource dataSource;

    static{
     dataSource = new DB2SimpleDataSource();   
     dataSource.setDatabaseName("DBNAME");
     dataSource.setServerName("xxx.xxx.xxx.xxx");
     dataSource.setPortNumber(447);
     dataSource.setUser("USER");
     dataSource.setPassword("PASS");
     dataSource.setDriverType(4);
     dataSource.setCurrentSchema("SCHEMA");
         //OR even better get the DataSource through JNDI lookup if defined on server
    }

    public static Connection getConnection() throws SQLException{
         return dataSource.getConnection()
    }
}
codependent
  • 23,193
  • 31
  • 166
  • 308
  • Singleton **is not** the proper solution here. **Never**. **Ever**. – Luiggi Mendoza Jun 23 '14 at 07:07
  • Would this approach work if i have web-application, like application being in web container; how to deploy this into the application server so that this module/service gets loaded at the time of AS start up? – CuriousMind Jun 23 '14 at 07:09
  • @LuiggiMendoza: Can you please suggest what we do in real big projects. Thanks a lot – CuriousMind Jun 23 '14 at 07:10
  • @vipinkoul I did it in my comment on your question as a reply to AnshuKunal. The result is the same: use a database connection pool. It's up to the designers of your application to define its creation and use: through a configuration in the application server or through *manual* configuration. – Luiggi Mendoza Jun 23 '14 at 07:12
  • @LuiggiMendoze How come singleton is not the right approach?? What about Spring Framework which is completely based on singletons?? – codependent Jun 23 '14 at 07:13
  • @vipin koul Definitely it would work in a web application. The first time the class is loaded the static block would initialize the DataSource so it would be available anywhere within your application – codependent Jun 23 '14 at 07:15
  • An application server already has a way for defining and retrieving datasources. Why would you reinvent the wheel (badly). – Mark Rotteveel Jun 23 '14 at 07:16
  • @WornOutSoles Spring framework **does not** use Singleton pattern, it uses [flyweight pattern](http://en.wikipedia.org/wiki/Flyweight_pattern) because the *singleton* beans have a unique instance **per context**, also you can create a new instance of those *singleton* beans manually, which breaks the concept of Singleton Pattern. – Luiggi Mendoza Jun 23 '14 at 07:18
  • @MarkRotteveel I won't do it for integration testing purposes and if the application has to be executed off side an application server. – Luiggi Mendoza Jun 23 '14 at 07:19
  • @LuiggiMendoza Even then JNDI can be used to obtain a `DataSource`, even if that JNDI is not part of the application server – Mark Rotteveel Jun 23 '14 at 07:23
  • @MarkRotteveel yes I know, but why to even store the connections in JNDI when you don't really need it? – Luiggi Mendoza Jun 23 '14 at 07:24
  • @LuiggiMendoza Given the original question, it seems like the better choice to me. – Mark Rotteveel Jun 23 '14 at 07:38