0Pricing
Neo4j Graph Database Fundamentals · درس

التعامل مع المعاملات والجلسات

افهم كيفية إدارة المعاملات والجلسات في تعليمات تطبيقك البرمجية لإجراء تفاعلات قوية وموثوقة مع قاعدة البيانات

التعامل مع المعاملات والجلسات درس مجاني في Neo4j Graph Database Fundamentals على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Neo4j Graph Database Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Neo4j Graph Database Fundamentals 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Transactions Matter

When you update data in a database, especially critical information, you want to ensure the operation is reliable. This is where transactions come in.

Transactions help maintain data integrity and consistency, even if something goes wrong during an operation.

Understanding Transactions

A transaction is a single, logical unit of work. It's a sequence of operations performed as a single atomic operation.

Think of it like transferring money between bank accounts: either both the debit and credit happen, or neither does. You wouldn't want money to leave one account without arriving in another!

The ACID Test

Transactions are often described by their ACID properties:

  • Atomicity: All or nothing. Either the entire transaction succeeds, or none of it does.
  • Consistency: Ensures the database moves from one valid state to another.
  • Isolation: Concurrent transactions don't interfere with each other.
  • Durability: Once a transaction is committed, its changes are permanent.

Connecting with Sessions

In Neo4j, you interact with the database using sessions. A session manages the communication channel and provides methods to execute Cypher queries.

Sessions are lightweight and designed to be opened and closed frequently. They are your primary interface for running queries, including those within transactions.

Basic Session Read

For simple read operations, you often don't need explicit transaction management. The Neo4j driver handles this for you with a read transaction.

Here's how to fetch a node count using a session:

from neo4j import GraphDatabase

URI = "bolt://localhost:7687"
USERNAME = "neo4j"
PASSWORD = "password"

driver = GraphDatabase.driver(URI, auth=(USERNAME, PASSWORD))

def get_node_count(tx):
    result = tx.run("MATCH (n) RETURN count(n) AS count")
    return result.single()["count"]

with driver.session() as session:
    count = session.read_transaction(get_node_count)
    print(f"Total nodes in graph: {count}")

driver.close()

Explicit Write Transactions

For operations that modify the database (like CREATE, MERGE, SET, DELETE), it's best practice to use explicit write transactions. This ensures atomicity.

The write_transaction method automatically handles committing if successful, or rolling back if an error occurs.

from neo4j import GraphDatabase

URI = "bolt://localhost:7687"
USERNAME = "neo4j"
PASSWORD = "password"

driver = GraphDatabase.driver(URI, auth=(USERNAME, PASSWORD))

def create_person(tx, name):
    tx.run("CREATE (p:Person {name: $name})", name=name)
    print(f"Created Person: {name}")

with driver.session() as session:
    session.write_transaction(create_person, "Alice")
    session.write_transaction(create_person, "Bob")

driver.close()

Commit or Rollback?

When using explicit transactions, your changes are not permanently saved until you commit them. If an error occurs, the transaction is rolled back, undoing all changes.

The session.write_transaction() and session.read_transaction() methods handle this implicitly for you. If the function passed to them completes without error, it commits. If an exception is raised, it rolls back.

Robust Error Handling

It's crucial to handle errors within your transaction logic. Any unhandled exception will cause the transaction to roll back, preventing partial updates.

You can use standard Python try...except blocks within your transaction function to manage specific errors or perform cleanup.

from neo4j import GraphDatabase

URI = "bolt://localhost:7687"
USERNAME = "neo4j"
PASSWORD = "password"

driver = GraphDatabase.driver(URI, auth=(USERNAME, PASSWORD))

def create_unique_node(tx, label, name):
    try:
        # Attempt to create a node. For demo, we'll re-raise to show rollback
        tx.run(f"CREATE (n:{label} {{name: $name}})", name=name)
        print(f"Node created: {name}")
    except Exception as e:
        print(f"Error creating node {name}: {e}")
        # Re-raise to trigger rollback for the entire write_transaction
        raise 

with driver.session() as session:
    # First call will likely succeed (unless node exists)
    try:
        session.write_transaction(create_unique_node, "City", "London")
    except Exception:
        print("Transaction for 'London' rolled back due to error.")

    # Second call, intentionally designed to fail for demonstration
    # (e.g., if we had a unique constraint on City.name)
    try:
        # Forcing an error to demonstrate rollback
        def force_error(tx):
            tx.run("CREATE (x)")
            raise ValueError("Simulated error!")
        session.write_transaction(force_error)
    except Exception:
        print("Transaction rolled back due to simulated error.")

driver.close()

Pythonic Context Managers

For even finer-grained control, or when performing multiple operations within a single transaction, you can use the session as a context manager and explicitly manage the transaction object.

The session.begin_transaction() method returns a transaction object that also works as a context manager.

from neo4j import GraphDatabase

URI = "bolt://localhost:7687"
USERNAME = "neo4j"
PASSWORD = "password"

driver = GraphDatabase.driver(URI, auth=(USERNAME, PASSWORD))

with driver.session() as session:
    with session.begin_transaction() as tx:
        tx.run("CREATE (a:Item {id: 1, name: 'Laptop'})")
        tx.run("CREATE (b:Item {id: 2, name: 'Mouse'})")
        # If no error, transaction commits automatically here when 'with tx' block exits
        print("Two items created in one transaction.")

driver.close()

Transaction Challenge

Consider a scenario where you are updating two properties of a single node in Neo4j within a Python application. If the second update fails due to a network error, what should happen to the first update if it was part of the same explicit write transaction?

Recap & Next Steps

We've covered the critical role of transactions in maintaining data integrity and consistency in your Neo4j applications.

  • Transactions follow ACID properties.
  • You use sessions to interact with the database.
  • session.read_transaction() and session.write_transaction() provide convenient, atomic operations.
  • Errors within a transaction lead to an automatic rollback, ensuring no partial data is committed.
  • Context managers offer a Pythonic way to manage sessions and transactions.

Understanding transactions is key to building robust and reliable Neo4j applications!

الأسئلة الشائعة

هل درس «التعامل مع المعاملات والجلسات» مجاني؟

نعم — نص درس «التعامل مع المعاملات والجلسات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Neo4j Graph Database Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Neo4j Graph Database Fundamentals 4 دروس في المجموع.

ماذا ستتعلم في «التعامل مع المعاملات والجلسات»؟

افهم كيفية إدارة المعاملات والجلسات في تعليمات تطبيقك البرمجية لإجراء تفاعلات قوية وموثوقة مع قاعدة البيانات تتمرن على Neo4j Graph Database Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Neo4j Graph Database Fundamentals؟

لا تُشترط خبرة سابقة. Neo4j Graph Database Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «التعامل مع المعاملات والجلسات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Neo4j Graph Database Fundamentals هذا؟

نعم. كل درس في Neo4j Graph Database Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الاتصال باستخدام برنامج تشغيل Python
  2. تنفيذ عمليات CRUD برمجيًا
  3. التعامل مع المعاملات والجلسات
  4. تجميع الاتصالات ومعالجة الأخطاء
← العودة إلى Neo4j Graph Database Fundamentals