30 Ekim 2017 Pazartesi

DataSource Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.sql.DataSource;
Bu sınıf DriverManager  ile aynı işi görür. Açıklaması şöyle
The DataSource interface is implemented by a driver vendor. There are three types of implementations:
Basic implementation -- produces a standard Connection object
Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.
Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.
constructor - JNDI
Uygulama sunucusunu kullanarak şöyle yaparız.
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
Şöyle yaparız.
DataSource ds = (DataSource) new InitialContext().lookup("jdbc/myDS");
constructor - C3P0
Şu satırı dahil ederiz.
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>4.2.20.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
</dependency>
Bazı değişkenler şöyle
minPoolSize=10
maxPoolSize=50
maxStatements=50
idleConnectionTestPeriod=3600
acquireIncrement=5
maxIdleTime=600
numHelperThreads=6
unreturnedConnectionTimeout=3600
maxIdleTimeExcessConnections=600
Şöyle yaparız.
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("swaldman");
cpds.setPassword("test-password");

// the settings below are optional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);

// The DataSource cpds is now a fully configured and usable pooled DataSource 
constructor - DBCP
Şöyle yaparız.
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver")
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);
...
Connection conn = ds.getConnection();
constructor - MySql
Maven için şöyle yaparız.
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
MySql JDBC sürücüsünün ismi MySQL Connector/J'dir. Bu jar dosyasının CLASSPATH içinde olması gerekir. Şöyle yaparız.
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("scott");
dataSource.setPassword("tiger");
dataSource.setServerName("myDBHost.example.org");
constructor - Oracle
OracleDataSource Sınıfı yazısına taşıdım.

constructor - Oracle Universal Connection Pool
Açıklaması şöyle
Prior to 12c (i.e., 12.1.0.1.0), UCP could work with any version of Oracle JDBC driver. With the new pool, UCP 12.1.0.2, it is dependent on Oracle JDBC driver 12.1.0.2.
Şöyle yaparız.
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();

pds.setURL(...);
pds.setUser(...);
pds.setPassword(...);
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");

//Setting pool properties
pds.setInitialPoolSize(5);
pds.setMinPoolSize(5);
pds.setMaxPoolSize(20);

Connection conn = pds.getConnection();
Şöyle yaparız.
int usedConnectionCount = pds.getBorrowedConnectionsCount();
int availableConnectionCotun = pds.getAvailableConnectionsCount();
constructor - postgresql
Maven için şöyle yaparız.
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.4-1200-jdbc4</version>
</dependency>
constructor - tomcat
Tomcat JDBC Connection Pool yazısına  taşıdım.

getConnection metodu
Örnek
Şöyle yaparız.
Connection con = dataSource.getConnection();
Örnek
Şöyle yaparız.
try (Connection connnection = dataSource.getConnection()) {
  // use the connection
  connection.commit();
}



Hiç yorum yok:

Yorum Gönder