What DataSource for the application is used both in standalone and in the context of webapp (Java 7, Tomcat 7)?

I am creating an application that has a standalone "core" level, which is also used to create webapp (this is a multi-module Maven project with the "core" module and the "webapp" module, which has a dependency on the "core" module). It uses a MySQL database. I am trying to implement DataSourcewhat would be good in both contexts (just one connection in an autonomous context is enough).

After reading many docs about DataSources, I have to say that I'm a little lost. I concluded that maybe I should use the Tomcat JDBC Connection Pool . My questions:

1) in a stand-alone context, how should I provide a configuration for using a DataSource, knowing that this configuration will be provided by Tomcat in the webapp context (a stand-alone configuration should not, therefore, override Tomcat's configuration)?

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

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

2) What about other federated DataSource implementations?

: , , , " " DataSource. .

+5
1

DataSource . JNDI Datasource Tomcat webapp JNDI .

. Connection :

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

. Tomcat - .

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

, ctx.lookup :

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

InitialContextFactory .

com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource . . Tomcat DBCP Apache Software Foundation, Java, Java: http://jakarta.apache.org/commons/dbcp/

JDBC Tomcat . this Apache DBCP . .

+2

All Articles