---
schema_version: "1"
id: c709cbd63afd162099d64ab191a0fbd362f207f55daa75dc852fc777c5865955
title: UseWithSharingOnDatabaseOperation (Retired)
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/code-analyzer/salesforce-code-analyzer/guides/v3x/salesforce-graph-engine/usewithsharingondatabaseoperation-rule.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/guide/usewithsharingondatabaseoperation-rule.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 1c7a26d92ad39aad239babc9ee45b7e464ba4de5b3f8c31cbd1c5b1f1570d78c
products:
  - Salesforce Code Analyzer
---
# UseWithSharingOnDatabaseOperation (Retired)

The UseWithSharingOnDatabaseOperation rule identifies database operations in classes annotated as `without sharing`. It also warns of database operations in classes that inherit with sharing implicitly instead of explicitly using `inherited sharing`.

With Salesforce sharing rules, you can control who has access to which records, but it's your responsibility to ensure that your Apex code respects sharing rules by declaring classes with a `sharing` model.

- `with sharing` causes database transactions in a class to respect sharing rules. It's the default recommendation.
- `without sharing` causes database transactions in a class to ignore sharing rules. Use with caution.
- `inherited sharing` causes database transactions in a class to inherit the sharing model of the class that called it. Use for classes that require flexibility.

To protect user data in Apex, use `with sharing` or `inherited sharing` whenever possible.

## Definition

- Source

  - : `@AuraEnabled`-annotated methods.
    `@InvocableMethod`-annotated methods.
    `@NamespaceAccessible`-annotated methods.
    `@RemoteAction`-annotated methods.
    Any method returning a `PageReference` object.
    `public`-scoped methods on Visualforce Controllers.
    `global`-scoped methods on any class.
    `Messaging.InboundEmailResult handleInboundEmail()` methods on implementations of `Messaging.InboundEmailHandler`.
    Any method targeted during invocation.

- Sink

  - : Any database operation.

- Sanitizer

  - : Class-level with sharing or inherited sharing annotation.

## Interpreting Results

Match any violation message that you receive with these cases to understand more about the violation.

### Common Case: Error Message

> Database operation must be executed from a class that enforces sharing rules.

The database operation occurs in a `without sharing` context, either because it occurs in a class annotated `without sharing` or because its class inherits sharing from a `without sharing` class. To resolve this violation, add `with sharing` or `inherited sharing` to the class.

### Common Case: Warning Message

> The database operation’s class implicitly inherits a sharing model from %s %s. Explicitly assign a sharing model instead.

This warning is thrown when a database operation occurs in a class that has no explicitly declared sharing model, so it implicitly inherits `with sharing` from its calling class. Even though the operation is secure in this specific case, it isn’t secure by default. Explicitly assign this class a sharing model to make it secure by default.
