Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which DataSource for application used both in standalone and webapp context (Java 7, Tomcat 7)?

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?

  • Shouldn't I just use the MySQL DataSource?

  • I read that in Java EE 7, DataSources are simpler to use (https://blogs.oracle.com/arungupta/entry/default_datasource_in_java_ee). So, is there any breakthrough in Java 7? A new DataSource provider that I should absolutely use in Java 7?

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

like image 439
FBB Avatar asked Nov 04 '22 03:11

FBB


1 Answers

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.

like image 107
Paul Vargas Avatar answered Nov 10 '22 19:11

Paul Vargas