DataSource: Difference between revisions
Line 18: | Line 18: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
import javax.sql.DataSource; | import javax.sql.DataSource; | ||
... | ... | ||
Line 33: | Line 34: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
import javax.sql.DataSource; | import javax.sql.DataSource; | ||
... | ... | ||
DataSource ds = (DataSource)ic.lookup("java:/DefaultDS"); | DataSource ds = (DataSource)ic.lookup("java:/DefaultDS"); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 14:45, 25 September 2017
External
- https://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html
- https://docs.oracle.com/javase/7/docs/api/javax/sql/package-summary.html
Internal
Overview
This page refers to a JSE javax.sql.DataSource. The DataSource is part of JDBC and it is the main interface exposed by the javax.sql package. DataSource is part of JSE since 1.4. JDBC specifies that a database Connection can be obtained from a DriverManager or a DataSource. The DataSource is the preferred way of getting the Connection. JEE also mentions DataSource, defining it as a resource manager connection factory for java.sql.Connection objects.
Initialization
A DataSource instance can be injected by annotating a field or a method of a JEE component with the JEE @javax.annotation.Resource annotation:
import javax.sql.DataSource;
...
@Resource
DataSource ds;
@Resource
public void setDataSource(DataSource ds) {
...
}
A DataSource can also be looked up in JNDI:
import javax.sql.DataSource;
...
DataSource ds = (DataSource)ic.lookup("java:/DefaultDS");
TODO
Organizatorium
According to JDBC Javadoc, a DataSource is a factory for connections to the physical data source that this DataSource object represents.
An object that implements the DataSource interface will typically be registered with a naming service based on the JavaTM Naming and Directory (JNDI) API.
Example: {{{
DataSource ds = (DataSource)ic.lookup("java:/DefaultDS"); Connection conn = ds.getConnection();
....
conn.close();
}}}