Database methods with a true parameter will work same as DML statements. It won't allow partial DML operation in Apex
Why are we still using DML statement, when we can achieve the same functionality with Database method with a true parameter?
There are three main points, all of which are trivial.
insert records;
// or...
Database.insert(records, true);
public class Database {}
// ... Database.insert will now fail to compile
System.Database.insert(records); // grumble grumble
Declaring a variable or class named Database can render your code crippled. Inline DML keywords cannot be shadowed and will never be mistaken for a data type.
Almost everyone that's used Apex Code has used the inline methods, while the Database methods they may not be as familiar with. Similarly, I believe they're slightly easier to read when casually reading code, since we're used to them.
Overall, there's no significant advantage of using the inline statements. Sure, we can use them, but there's no particularly compelling reason to do so. However, there's also no reason to go out of your way to avoid inline statements, so feel free to use them as you want to.
The typing benefit isn't significant (~20 characters), and developers should not be creating a class named Database, so the second statement shouldn't matter either.
For what it's worth, the inline DML syntax appears to use more CPU time than the Database method (~0.014 milliseconds), which is also a trivial amount of time, considering we're limited to 150 statements per transaction.
Use DML statements if you want any error that occurs during bulk DML processing to be thrown as an Apex exception that immediately interrupts control flow (by using try. . .catch blocks). This behavior is similar to the way exceptions are handled in most database procedural languages. Using Database class methods you will have to iterate on the save-result list to identify the error.
false
for the allOrNone parameter, which is the opposite of what the OP is asking about.
Database
methods return a list of results (SaveResult
for insert/update for example) wich returns the errors.Database
methods.