من DriverManager إلى DataSource
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
get database connections from a pool instead of opening one per statement.
الموضوعات المخطط لها
- JDBC is Java's standard API to talk to SQL databases; a driver (JAR) speaks the database's protocol.
DriverManager.getConnection(...)opens a brand-new connection each time: slow (network, login, setup).- A
DataSourcehands out connections; a pool (for example HikariCP) keeps them open and reuses them. - "Closing" a pooled connection returns it to the pool; always use try-with-resources.
- Never open one connection per statement.
تفاصيل المخطط المبدئي
عرض عملي
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("select id, name from product where id = ?")) {
ps.setLong(1, 42L);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) System.out.println(rs.getString("name"));
}
}اختبر فهمك
What does close() do on a pooled connection?
أظهر الإجابة
Returns it to the pool; the real connection stays open.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.