Giriş
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
import oracle.jdbc.pool.OracleDataSource;
DataSource arayüzünü gerçekleştirir.Kullanım
1. Default constructor ile nesne yaratılır
1. Default constructor ile nesne yaratılır
2. setX metodları ile bağlantı ayarları belirtilir.
3. setX metodlarını teker teker çağırmak istemiyorsak, setConnectionProperties() ile hepsi bir Property nesnesi olarak geçilir.
4. setXEnabled metodları ile bazı özellikler etkinleştirilir.
5. getConnection() ile connection alınır
constructor
Şöyle yaparız.
OracleDataSource dataSource = new OracleDataSource();
getConnection metoduŞöyle yaparız.
Connection connection = dataSource.getConnection();
setConnectionProperties
Şöyle yaparız
import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
Properties info = new Properties();
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
info.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, false);
OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(info);
setDriverType metodu
OracleDataSource dataSource = new OracleDataSource();
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setURL(dataSourceUrl);
dataSource.setImplicitCachingEnabled(true);
dataSource.setFastConnectionFailoverEnabled(true);
dataSource.setDriverType(driverClass);
setFastConnectionFailoverEnabled metoduBu metod yeni sürümlerde artık yok. Açıklaması şöyle. Veri tabanı kapanıp açılırsa geçersiz hale gelen bağlantılar temizlenir.
When the DB restarts, do you remove the invalid connections from the pool using a ClearPool type API or Oracle Fast Connection Failover (FCF)? If the answer is no, that's the likely reason for the timeout or error when pooling is on.Şöyle yaparız.
The invalid connections remain in the pool and the app is picking up one of these connections.
The ClearPool APIs and/or FCF are intended to resolve this HA issue. ClearPool is a manual process unfortunately and is not a great solution for this specific HA situation. Most customers prefer to automate connection cleanup with FCF.
To use FCF, your DB needs to enable Fast Application Notification, then turn on FCF on the server side. On the client side, you just turn HA Events=true in the connection string, which should already be turned on by default if you are using ODP.NET 12.2.
dataSource.setFastConnectionFailoverEnabled(true);
setImplicitCachingEnabled metodu
Şöyle yaparız.dataSource.setImplicitCachingEnabled(true);
setPassword metoduŞöyle yaparız.
String password =...;
dataSource.setPassword(password);
Şöyle yaparız.
String url = "jdbc:oracle:thin:scott/tiger@//myhost:1521/orcl");
dataSource.setURL(url);
Bu metod yerine ayrı ayrı setter'lar kullanılabilir. Şöyle yaparız.dataSource.setServerName("xxx");
dataSource.setPortNumber(xxx);
dataSource.setDriverType("thin");
dataSource.setNetworkProtocol("tcp");
dataSource.setDatabaseName("xxxx");
setUser metoduŞöyle yaparız.
String username = ...;
dataSource.setUser(username);
DiğerÖrnek
Şöyle yaparız.
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="dataSourceName" value="ds" />
<property name="URL"
value="jdbc:oracle:thin:@127.0.0.1:1521:test" />
<property name="user" value="system" />
<property name="password" value="3123312257" />
</bean>
ÖrnekŞöyle yaparız.
@Bean
DataSource dataSource() throws SQLException {
OracleDataSource dataSource = new OracleDataSource();
dataSource.setUser("yourusername");
dataSource.setPassword("yourpassword");
dataSource.setURL("jdbc:oracle:thin:@yourserver:1521:xe");
dataSource.setImplicitCachingEnabled(true);
dataSource.setFastConnectionFailoverEnabled(true);
return dataSource;
}
Örnek
Şöyle yaparız
import oracle.jdbc.pool.OracleDataSource;
public static void main(String[] args) {
OracleDataSource ods;
try {
ods = new OracleDataSource();
// jdbc:oracle:thin@[hostname]:[port]/[DB service/name]
ods.setURL("jdbc:oracle:thin:@localhost:1521/FREEPDB1");
ods.setUser("[Username]");
ods.setPassword("[Password]");
Connection conn = ods.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT 'Hello World!' FROM dual");
ResultSet rslt = stmt.executeQuery();
while (rslt.next()) {
System.out.println(rslt.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}