5

I build an application which has a standalone "core" layer, that is also used to build a webapp (it is a Maven multi-module project, with a 'core' module, and a 'webapp' module, that has a dependency to the 'core' module). It uses a MySQL database. I try to implement a DataSource that would be fine in both contexts (only 1 connection is enough in the standalone context).

After reading a lot of documentations about DataSources, I have to say that I am a bit lost. I came to the conclusion that maybe I should use the Tomcat JDBC Connection Pool. My questions are:

1) in the standalone context, how should I provide the configuration to use the DataSource, knowing that this configuration will be provided by Tomcat in the webapp context (the standalone config should thus not override the Tomcat config)?

  • Should I do something like in this other question in a method called only in the standalone context? But how can I see in the webapp context that Tomcat has already provided the DataSource?

  • Or Should I rather use a bean definition? But how this bean would not be used in the webapp context?

2) What about other pooled DataSource implementations?

Conclusion: Yep, I'm lost, and I don't know what are the "gold standards" for DataSource usage. Thank you for your help.

Community
  • 1
  • 1
FBB
  • 1,414
  • 2
  • 17
  • 29

1 Answers1

2

You can lookup the DataSource in the core module. You need create the JNDI Datasource in Tomcat for the webapp module and the Standalone JNDI support for standalone module.

e.g. For getting the Connection from datasource is the same in both context:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
Connection conn = ds.getConnection();

But the configuration of datasource is diferent. In Tomcat you will see something like that in the examples in the links.

<Context>
<Resource name="jdbc/MySQLDB" ... />
</Context>

The name of resource in the standalone must be she same like the ctx.lookup call in standalone:

Properties prop = new Properties();
prop.put("java:comp/env/jdbc/MySQLDB", ds1);

The InitialContextFactory in the standalone module must be created independently of core module.

About the com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource see this. Tomcat uses the Apache Software Foundation's DBCP libraries, which you can also use in your own non-Java EE code: http://jakarta.apache.org/commons/dbcp/

For JDBC Connection Pool in Tomcat see this and for Apache DBCP Pool see this.

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Thanks for the reply, a few questions then: my 'core' layer is a library that can be used in other applications. If I don't want these applications to perform the tasks described in your second link, the InitialContext should be set **inside** the core layer, right? So maybe I should more simply perform the lookup in the core layer, catch the exception thrown if not in a webapp context, then set my own Connection in that case? Would that be valid? – FBB Mar 05 '13 at 00:39
  • If the 'core' contains such functionality, that could be executed by other applications. You could generate a third component (with the custom `InitialContextFactory` ) that could be called from standalone application. I think that this requirement functionality may be unique to the standalone application, so you could embed it in such an application. In another hand, you could implement a connection factory. – Paul Vargas Mar 05 '13 at 01:49
  • Yep, just a simple connection factory using a DriverManager, when the DataSource cannot be loaded using JNDI. The Driver could be configured through a property file. Would that be valid? Os is it a terrible design pattern? :p – FBB Mar 05 '13 at 12:42