A DataSource allows you to get a JDBC connection primarily from a connection pool. A DataSource object represents a specific DBMS or some other data source, such as a file. If a company uses more than one data source, it will deploy a separate DataSource for each of them. The DataSource interface is implemented by the driver provider. You export the DB connection properties file and retrieve the object using JNDI. Using a DataSource , you only need to know the JNDI name. The application server takes care of the details.
It can be implemented in three different ways:
- The
DataSource implementation creates standard Connection objects that are not merged or used in a distributed transaction. - An A
DataSource implementation that supports pooling creates Connection objects that participate in the connection pool, that is, connections that can be processed. - An A
DataSource implementation that supports distributed transactions creates connection objects that can be used in a distributed transaction, that is, a transaction that accesses two or more DBMS servers.
As in Spring, you can configure the data source in an XML file, and then (1) either enter it into your bean, (2) get it from the ApplicationContext .
DataSource ds = (DataSource) ApplicationContextProvider. getApplicationContext().getBean("myDataSource"); Connection c = ds.getConnection();
Recommended reading:
source share