avatarAdam Szpilewicz

Summary

The go-ora library provides a pure Go solution for Golang developers to connect to Oracle Database without the need for an Oracle Client.

Abstract

The article discusses the use of the go-ora library as an efficient method for Golang developers to connect to Oracle Database. It outlines a step-by-step process for setting up a connection using this library, which eliminates the need for an Oracle Client. The process includes importing necessary packages, creating a connection function with database credentials, and testing the connection. The article emphasizes the simplicity and performance benefits of go-ora, such as the ability to prefetch rows to optimize large queries, and positions it as a valuable tool for those working with Oracle Databases in Golang.

Opinions

  • The author believes that go-ora is an essential tool for Golang developers working with Oracle Database due to its ease of use and efficiency.
  • The article suggests that go-ora is a preferable alternative to other solutions that may require additional dependencies or software installations.
  • The author expresses that using an SSH tunnel to forward the remote Oracle port to a local port is a viable method for connecting to the database.
  • The article implies that go-ora can improve performance for large queries by allowing developers to set the number of prefetch rows.
  • The author encourages readers who enjoy Medium articles to consider becoming a member, indicating a positive view of the Medium platform and community.

Connect to Oracle Database Using go-ora Library in Golang Without Oracle Client

Photo by BoliviaInteligente on Unsplash

As a Golang developer working with Oracle Database, it’s essential to find an efficient and straightforward way to connect to the database without the hassle of installing an Oracle Client. The go-ora library offers a pure Go solution for connecting to Oracle Database, making it more accessible for developers working in Golang.

In this article, we’ll walk you through how to use the go-ora library to connect to an Oracle Database without requiring an Oracle Client. We’ll explain the process step by step, using a sample function to illustrate the concepts.

Step 1: Import the necessary packages

Before we can use the go-ora library, we need to import the necessary packages. In this case, we need “database/sql” and “fmt” packages from the standard library, as well as “github.com/sijms/go-ora” for the go-ora library itself.

import (
	"database/sql"
	"fmt"
	_ "github.com/sijms/go-ora"
)

Step 2: Create the connect function

Now, we’ll create a function called connect that takes the password as an argument and returns a pointer to the sql.DB object and an error. The function sets up the connection to the Oracle Database and tests the connection using the Ping method.

func connect(password string) (*sql.DB, error) {
 // Replace these values with your database credentials
 username := "admin"
 serviceName := "ORACLE"

 // The SSH tunnel is forwarding the remote Oracle port to the local port 2525
 host := "localhost"
 port := 2525

 // Set the number of prefetch rows
 prefetchRows := 500

 // Create the connection string
 connStr := fmt.Sprintf(
  "oracle://%s:%s@%s:%d/%s?PREFETCH_ROWS=%d",
  username, password, host, port, serviceName, prefetchRows,
 )
 log.Printf("Opening connection to db using go-ora: %v", connStr)

 // Connect to the database
 db, err := sql.Open("oracle", connStr)
 if err != nil {
  return nil, err
 }

 // Test the connection
 err = db.Ping()
 if err != nil {
  db.Close()
  return nil, err
 }

 return db, nil
}

Step 3: Understanding the connect function

Let’s break down the connect function to understand how it works:

  1. Set the Oracle Database credentials (username and serviceName). Replace these values with your credentials.
  2. Define the host and port. In this example, we use an SSH tunnel to forward the remote Oracle port to the local port 2525.
  3. Set the number of prefetch rows. This value determines the number of rows fetched from the database in a single round-trip, improving performance for large queries.
  4. Create the connection string using the provided values.
  5. Open the connection to the database using sql.Open("oracle", connStr).
  6. Test the connection using the Ping method. If there’s an error, close the connection and return the error.

Conclusion

The go-ora library makes it easy to connect to an Oracle Database using Golang without the need for an Oracle Client. By following the steps outlined in this article, you can quickly set up a connection and start working with your Oracle Database using Golang. The go-ora library is an excellent alternative to other solutions that require additional dependencies or software installations, making it a valuable resource for Golang developers working with Oracle Databases.

If you enjoy reading medium articles and are interested in becoming a member, I would be happy to share my referral link with you!

https://medium.com/@adamszpilewicz/membership

Oracle
Database
Programming
Golang
Software Development
Recommended from ReadMedium