Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ import CodeBlock from '@theme/CodeBlock';
* [Get tracked entities](../../client-api/session/how-to/get-tracked-entities.mdx)
* [Check for session changes](../../client-api/session/how-to/check-if-there-are-any-changes-on-a-session.mdx)
* All the tracked changes are combined & persisted in the database only when calling `SaveChanges()`.
* Entity tracking can be disabled if needed. See:
* Entity tracking can be disabled if needed. See:
* [Disable entity tracking](../../client-api/session/configuration/how-to-disable-tracking.mdx)
* [Clear session](../../client-api/session/how-to/clear-a-session.mdx)
#### Create document example
* [Evict a single entity](../../client-api/session/how-to/evict-entity-from-a-session.mdx)

#### Create document example

* The Client API, and the Session in particular, is designed to be as straightforward as possible.
Open the session, do some operations, and apply the changes to the RavenDB server.
* The following example shows how to create a new document in the database using the Session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ import CodeBlock from '@theme/CodeBlock';
* Entity tracking can be disabled if needed. See:
* [Disable entity tracking](../../client-api/session/configuration/how-to-disable-tracking.mdx)
* [Clear session](../../client-api/session/how-to/clear-a-session.mdx)
* [Evict a single entity](../../client-api/session/how-to/evict-entity-from-a-session.mdx)

#### Create document example

* The Client API, and the Session in particular, is designed to be as straightforward as possible.
Open the session, do some operations, and apply the changes to the RavenDB server.
* The following example shows how to create a new document in the database using the Session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ import CodeBlock from '@theme/CodeBlock';
* Entity tracking can be disabled if needed. See:
* [Disable entity tracking](../../client-api/session/configuration/how-to-disable-tracking.mdx)
* [Clear session](../../client-api/session/how-to/clear-a-session.mdx)
* [Evict a single entity](../../client-api/session/how-to/evict-entity-from-a-session.mdx)

#### Create document example

* The Client API, and the session in particular, is designed to be as straightforward as possible.
Open the session, do some operations, and apply the changes to the RavenDB server.
* The following example shows how to create a new document in the database using the session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ import CodeBlock from '@theme/CodeBlock';
* Entity tracking can be disabled if needed. See:
* [Disable entity tracking](../../client-api/session/configuration/how-to-disable-tracking.mdx)
* [Clear session](../../client-api/session/how-to/clear-a-session.mdx)
* [Evict a single entity](../../client-api/session/how-to/evict-entity-from-a-session.mdx)

#### Create document example

* The Client API, and the Session in particular, is designed to be as straightforward as possible.
Open the session, do some operations, and apply the changes to the RavenDB server.
* The following example shows how to create a new document in the database using the Session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ import CodeBlock from '@theme/CodeBlock';
* Entity tracking can be disabled if needed. See:
* [Disable entity tracking](../../client-api/session/configuration/how-to-disable-tracking.mdx)
* [Clear session](../../client-api/session/how-to/clear-a-session.mdx)
* [Evict a single entity](../../client-api/session/how-to/evict-entity-from-a-session.mdx)

#### Create document example

* The Client API, and the Session in particular, is designed to be as straightforward as possible.
Open the session, do some operations, and apply the changes to the RavenDB server.
* The following example shows how to create a new document in the database using the Session.
Expand Down
106 changes: 86 additions & 20 deletions docs/client-api/session/how-to/_clear-a-session-csharp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,99 @@ import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';

To clear session state, stop tracking entities, and remove all pending commands,
use the `Clear` method from the `Advanced` session operations.
<Admonition type="note" title="">

## Syntax
* Use the `Clear()` method to clear the session’s state:
it **removes ALL tracked entities** and **cancels ALL pending operations** (e.g., _Store_, _Delete_).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But "removes ALL tracked entities" isn't something that is happening currently, right?

So we first need to check - https://issues.hibernatingrhinos.com/issue/RavenDB-25297/Load-after-Delete-Evict-does-not-trigger-a-server-call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arekpalinski
That issue (RavenDB-25297) only occurs when Evict is used in the flow.
I just re-ran this to test again, and from what I see -
when calling Clear, all entities are cleared, including those from the delete queue, as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right. It's about clearing. Everything okay then

This is useful when you need to discard all tracked changes and reset the session state.

* To remove only a **single entity** from tracking, see [Evict a single entity](../../../client-api/session/how-to/evict-entity-from-a-session.mdx).

* In this article:
* [Clear the session's state](../../../client-api/session/how-to/clear-a-session.mdx#clear-the-sessions-state)
* [Syntax](../../../client-api/session/how-to/clear-a-session.mdx#syntax)

</Admonition>

---

## Clear the session's state

<Tabs groupId='languageSyntax'>
<TabItem value="sync_session" label="sync_session">
```csharp
using (var session = store.OpenSession())
{
// Store a new employee
var newEmployee = new Employee
{
FirstName = "John",
LastName = "Doe"
};
session.Store(newEmployee, "employees/1");

// Load an existing employee and modify it
var existingEmployee = session.Load<Employee>("employees/2");
existingEmployee.LastName = "UpdatedLastName";

<TabItem value="clear_1" label="clear_1">
<CodeBlock language="csharp">
{`void Clear();
`}
</CodeBlock>
// Load another employee and mark for deletion
var employeeToDelete = session.Load<Employee>("employees/3");
session.Delete(employeeToDelete);

// At this point:
// * employees/1 is pending insert
// * employees/2 is pending update
// * employees/3 is pending delete

// Clear the session state
// this will remove all tracking and pending operations
session.Advanced.Clear();

// SaveChanges does nothing - all operations were discarded
session.SaveChanges();
}
```
</TabItem>
<TabItem value="async_session" label="async_session">
```csharp
using (var asyncSession = store.OpenAsyncSession())
{
// Store a new employee
var newEmployee = new Employee
{
FirstName = "John",
LastName = "Doe"
};
await asyncSession.StoreAsync(newEmployee, "employees/1");

## Example
// Load an existing employee and modify it
var existingEmployee = await asyncSession.LoadAsync<Employee>("employees/2");
existingEmployee.LastName = "UpdatedLastName";

<TabItem value="clear_2" label="clear_2">
<CodeBlock language="csharp">
{`session.Store(new Employee
\{
FirstName = "John",
LastName = "Doe"
\});
// Load another employee and mark for deletion
var employeeToDelete = await asyncSession.LoadAsync<Employee>("employees/3");
asyncSession.Delete(employeeToDelete);

session.Advanced.Clear();
// At this point:
// * employees/1 is pending insert
// * employees/2 is pending update
// * employees/3 is pending delete

session.SaveChanges(); // nothing will happen
`}
</CodeBlock>
// Clear the session state
// this will remove all tracking and pending operations
asyncSession.Advanced.Clear();

// SaveChangesAsync does nothing - all operations were discarded
await asyncSession.SaveChangesAsync();
}
```
</TabItem>
</Tabs>

## Syntax

<TabItem value="" label="">
```csharp
void Clear();
```
</TabItem>
71 changes: 52 additions & 19 deletions docs/client-api/session/how-to/_clear-a-session-nodejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,65 @@ import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';

To clear session state, stop tracking entities, and remove all pending commands,
use the `clear()` method from the `advanced` session operations.
<Admonition type="note" title="">

## Syntax
* Use the `clear()` method to clear the session’s state:
it **removes ALL tracked entities** and **cancels ALL pending operations** (e.g., _store_, _delete_).
This is useful when you need to discard all tracked changes and reset the session state.

* To remove only a **single entity** from tracking, see [Evict a single entity](../../../client-api/session/how-to/evict-entity-from-a-session.mdx).

<TabItem value="clear_1" label="clear_1">
<CodeBlock language="js">
{`session.advanced.clear();
`}
</CodeBlock>
</TabItem>
* In this article:
* [Clear the session's state](../../../client-api/session/how-to/clear-a-session.mdx#clear-the-sessions-state)
* [Syntax](../../../client-api/session/how-to/clear-a-session.mdx#syntax)

</Admonition>

---

## Clear the session's state

<TabItem value="sync_session" label="sync_session">
```js
const session = store.openSession();

// Store a new employee
class Employee {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
const newEmployee = new Employee("John", "Doe");
await session.store(newEmployee, "employees/1");

// Load an existing employee and modify it
const existingEmployee = await session.load("employees/2");
existingEmployee.lastName = "UpdatedLastName";

## Example
// Load another employee and mark for deletion
const employeeToDelete = await session.load("employees/3");
session.delete(employeeToDelete);

<TabItem value="clear_2" label="clear_2">
<CodeBlock language="js">
{`const employee = new Employee();
employee.firstName = "John";
employee.lastName = "Doe";
await session.store(employee);
// At this point:
// * employees/1 is pending insert
// * employees/2 is pending update
// * employees/3 is pending delete

// Clear the session state
// this will remove all tracking and pending operations
session.advanced.clear();

await session.saveChanges(); // nothing will hapen
`}
</CodeBlock>
// saveChanges does nothing - all operations were discarded
await session.saveChanges();
```
</TabItem>

## Syntax

<TabItem value="" label="">
```js
session.advanced.clear();
```
</TabItem>

Loading
Loading