DataSource: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 12: Line 12:
This page refers to a JSE [https://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html javax.sql.DataSource]. The DataSource is part of [[JDBC]] and it is the main interface exposed by the <tt>javax.sql</tt> 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 [[JCA_Concepts#Resource_Manager|resource manager]] connection factory for <tt>java.sql.Connection</tt> objects.
This page refers to a JSE [https://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html javax.sql.DataSource]. The DataSource is part of [[JDBC]] and it is the main interface exposed by the <tt>javax.sql</tt> 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 [[JCA_Concepts#Resource_Manager|resource manager]] connection factory for <tt>java.sql.Connection</tt> objects.


=Initialization=
=DataSource Instance Initialization=


A DataSource instance can be injected by annotating a field or a method of a JEE component with the JEE [[@javax.annotation.Resource|@javax.annotation.Resource]] annotation:
A DataSource instance can be injected by annotating a field or a method of a JEE component with the JEE [[@javax.annotation.Resource|@javax.annotation.Resource]] annotation:
Line 21: Line 21:
...
...


@Resource
@Resource(name="java:/DefaultDS")
DataSource ds;
DataSource ds;


@Resource
@Resource(name="java:/DefaultDS")
public void setDataSource(DataSource ds) {
public void setDataSource(DataSource ds) {
   ...
   this.ds = ds;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 42: Line 42:
</syntaxhighlight>
</syntaxhighlight>


=TODO=
=Connection Life Cycle=


* https://home.feodorov.com:9443/wiki/Wiki.jsp?page=JBossDataSource
<syntaxhighlight lang='java'>
import java.sql.Connection;
 
...
 
DataSource ds;


=Organizatorium=
Connection c;


try {


  c = ds.getConnection();
 
  ...


According to JDBC Javadoc, a DataSource is a ''factory for connections'' to the physical data source that this DataSource object represents.
}
finally {


An object that implements the DataSource interface will typically be registered with a naming service based on the JavaTM Naming and Directory (JNDI) API.
  if (c != null) {


Example:
      c.close();
{{{
  }
      DataSource ds = (DataSource)ic.lookup("java:/DefaultDS");
}
      Connection conn = ds.getConnection();
</syntaxhighlight>


      ....
=JDBC API Usage=


      conn.close();
{{Internal|JDBC API Usage|JDBC API Usage}}
}}}

Latest revision as of 14:47, 26 September 2017

External

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.

DataSource Instance 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(name="java:/DefaultDS")
DataSource ds;

@Resource(name="java:/DefaultDS")
public void setDataSource(DataSource ds) {
   this.ds = ds;
}

For more details on attributes supported by the @Resource annotation see:

@javax.annotation.Resource

A DataSource can also be looked up in JNDI:

import javax.sql.DataSource;

...

DataSource ds = (DataSource)ic.lookup("java:/DefaultDS");

Connection Life Cycle

import java.sql.Connection;

...

DataSource ds;

Connection c;

try {

  c = ds.getConnection();
  
  ...

}
finally {

  if (c != null) {

      c.close();
  }
}

JDBC API Usage

JDBC API Usage