تجميع الاتصالات ومعالجة الأخطاء
تعلّم إدارة تجمعات اتصالات برنامج تشغيل Neo4j بكفاءة، ومعالجة الأخطاء وإعادة المحاولات عند دمج Neo4j مع التطبيقات.
تجميع الاتصالات ومعالجة الأخطاء درس مجاني في Neo4j Graph Database Fundamentals على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Neo4j Graph Database Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Neo4j Graph Database Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Driver Is Long-Lived
A common mistake is creating a new driver per request. The Neo4j driver manages a connection pool and should be created once and reused for your app's lifetime.
from neo4j import GraphDatabase
driver = GraphDatabase.driver('neo4j://localhost:7687', auth=('neo4j', 'password'))
# reuse driver everywhere; close at shutdownWhat a Connection Pool Does
The driver keeps a pool of open connections and hands them out to sessions on demand. This avoids the cost of opening a TCP connection per query.
Tuning Pool Size
You can configure the maximum pool size and connection lifetime to match your workload.
driver = GraphDatabase.driver(
uri,
auth=auth,
max_connection_pool_size=50,
max_connection_lifetime=3600
)Sessions Are Cheap
Unlike drivers, sessions are lightweight. Open a session per unit of work and close it promptly, ideally with a context manager.
with driver.session() as session:
result = session.run('MATCH (n) RETURN count(n) AS c')
print(result.single()['c'])Transient vs Permanent Errors
Neo4j errors are classified. Transient errors (like a leader switch in a cluster) can be safely retried. Permanent errors (like syntax errors) cannot.
Automatic Retries
Managed transaction functions automatically retry on transient errors, which is why they are the recommended way to run writes.
def add_person(tx, name):
tx.run('CREATE (:Person {name: $name})', name=name)
with driver.session() as session:
session.execute_write(add_person, 'Alice')Catching Driver Exceptions
Wrap calls to handle failures gracefully and surface useful messages to your app.
from neo4j.exceptions import ServiceUnavailable, CypherSyntaxError
try:
with driver.session() as s:
s.run('MATCH (n) RETURN n')
except ServiceUnavailable:
print('Database unreachable')
except CypherSyntaxError as e:
print('Bad query:', e)Timeouts
Set transaction timeouts so a slow query does not hold a connection forever, freeing the pool for other work.
with driver.session() as s:
s.run('MATCH (n) RETURN n', timeout=5)Closing Resources
Always close the driver on application shutdown to release pooled connections cleanly.
import atexit
atexit.register(driver.close)Pool Exhaustion
If all connections are busy, new requests wait. Symptoms include rising latency. Fixes: close sessions promptly, raise pool size, or shorten slow queries.
Production Checklist
For robust integration:
- One shared driver instance
- Short-lived sessions
- Managed transactions for retries
- Timeouts and exception handling
- Close driver on shutdown
Quick Check
Test your connection management knowledge.
Recap
You learned robust driver usage:
- Reuse a single long-lived driver
- Sessions are cheap; open and close per task
- Managed transactions retry transient errors
- Handle exceptions and set timeouts
- Avoid pool exhaustion with prompt cleanup
الأسئلة الشائعة
هل درس «تجميع الاتصالات ومعالجة الأخطاء» مجاني؟
نعم — نص درس «تجميع الاتصالات ومعالجة الأخطاء» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Neo4j Graph Database Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Neo4j Graph Database Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «تجميع الاتصالات ومعالجة الأخطاء»؟
تعلّم إدارة تجمعات اتصالات برنامج تشغيل Neo4j بكفاءة، ومعالجة الأخطاء وإعادة المحاولات عند دمج Neo4j مع التطبيقات. تتمرن على Neo4j Graph Database Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Neo4j Graph Database Fundamentals؟
لا تُشترط خبرة سابقة. Neo4j Graph Database Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تجميع الاتصالات ومعالجة الأخطاء»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Neo4j Graph Database Fundamentals هذا؟
نعم. كل درس في Neo4j Graph Database Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الاتصال باستخدام برنامج تشغيل Python
- تنفيذ عمليات CRUD برمجيًا
- التعامل مع المعاملات والجلسات
- تجميع الاتصالات ومعالجة الأخطاء