Oracle ODP.NET with truly Async Methods in C#

Oracle's ODP.NET 23c Dev (or Oracle.ManagedDataAccess.Core) release brings genuine async/asynchronous methods to support to database interactions in C#, revolutionizing the way developers work with Oracle databases.

Under PreRelease until today : 2023-08-24

Oracle.ManagedDataAccess.Core - pre release

Async/Await in C#

Before delving into the specifics of ODP.NET 23c Dev for C#, let's take a moment to understand the power of asynchronous programming in C#.

The async/await pattern enables developers to write non-blocking code, freeing the main thread to perform other tasks while asynchronous operations are in progress.

This results in improved responsiveness, smoother user experiences, and efficient utilization of system resources.

Oracle ODP.NET 23c Dev: A New Era of Asynchronous Database Interaction

While previous versions of ODP.NET have async methods, a closer inspection reveals that they were mere wrappers around Task.FromResult calls.

These methods lacked true asynchronous behavior, limiting their potential in achieving optimal performance gains and making even worse then synchronous calls.

With the eagerly anticipated ODP.NET 23c Dev release, Oracle has taken a significant step forward by offering genuine async/await support, transforming the landscape of database interactions.

Pre Release until today - 2023-08-24

The issue #144 on Github was open since November 2021. We're expecting this feature for a very long time.

Implement Async versions of Ado.Net methods · Issue #144 · oracle/dotnet-db-samples
Ado.Net classes such as DbCommand, DbConnection have async methods that aren’t truly async and just execute the non-async versions and return their result. It would be nice if ODP.NET had a real im…

Key Async Methods in ODP.NET 23c Dev

Let's explore two pivotal async methods that are now available in their true asynchronous glory.

  • OpenAsync: Unlike its predecessor, the new OpenAsync method in ODP.NET 23c Dev truly opens a connection to the Oracle database asynchronously.
  • ExecuteReaderAsync: This method allows developers to execute queries and retrieve data from the Oracle database asynchronously.

Sample Async Database Interactions

Let's dive into some code examples that illustrate the difference between the old and the new:

// Opening a connection asynchronously (previous version)
using Oracle.ManagedDataAccess.Client;
// ...

async Task OpenConnectionAsync(string connectionString)
{
    using var connection = new OracleConnection(connectionString);
    
    await connection.OpenAsync();
    Console.WriteLine("Connection opened asynchronously.");
}

// Executing a query and reading data asynchronously (new ODP.NET 23c Dev version)
using Oracle.ManagedDataAccess.Client;
// ...

async Task ExecuteQueryAsync(string connectionString, string query)
{
    using var connection = new OracleConnection(connectionString);
    
    await connection.OpenAsync();
    
    using var command = new OracleCommand(query, connection);
    using var reader = await command.ExecuteReaderAsync();
    
    while (await reader.ReadAsync())
    {
    	// Process data asynchronously
    }
}

Embracing the Future: Leveraging ODP.NET 23c Dev for Superior Performance

The introduction of true asynchronous support in ODP.NET 23c Dev is a significant milestone for developers working with Oracle databases.

By embracing these new async methods, you can create applications that are more responsive, efficient, and user-friendly.

As you transition to the latest version of ODP.NET, you'll unlock the full potential of asynchronous programming and usher in a new era of high-performance database interactions.

References

How was the tutorial?

Love Discord?