comproto

package
v0.100.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FinishOnePhaseCommit

func FinishOnePhaseCommit(errp *error, cm OnePhaseCommitProtocol, tx context.Context)
Example
package main

import (
	"context"

	"github.com/adamluzsi/frameless/ports/comproto"
)

func main() {
	var cm comproto.OnePhaseCommitProtocol

	myMethod := func(ctx context.Context) (returnError error) {
		tx, err := cm.BeginTx(ctx)
		if err != nil {
			return err
		}
		defer comproto.FinishOnePhaseCommit(&returnError, cm, tx)
		// do something with in comproto
		return nil
	}

	_ = myMethod
}
Output:

func FinishTx

func FinishTx(errp *error, commit, rollback func() error)
Example
package main

import (
	"context"
	"database/sql"

	"github.com/adamluzsi/frameless/ports/comproto"
)

func main() {
	db, err := sql.Open(`fake`, `DSN`)
	if err != nil {
		panic(err)
	}

	myMethod := func(ctx context.Context) (returnError error) {
		tx, err := db.Begin()
		if err != nil {
			return err
		}
		defer comproto.FinishTx(&returnError, tx.Commit, tx.Rollback)
		// do something with in comproto
		return nil
	}

	_ = myMethod
}
Output:

Types

type OnePhaseCommitProtocol

type OnePhaseCommitProtocol interface {
	// BeginTx creates a context with a transaction.
	// All statements that receive this context should be executed within the given transaction in the context.
	// After a BeginTx command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given.
	//
	// In case the resource support some form of isolation level,
	// or other ACID related property of the transaction,
	// then it is advised to prepare this information in the context before calling BeginTx.
	// e.g.:
	//   ...
	//   var err error
	//   ctx = r.ContextWithIsolationLevel(ctx, sql.LevelSerializable)
	//   ctx, err = r.BeginTx(ctx)
	//
	BeginTx(context.Context) (context.Context, error)
	// CommitTx Commit commits the current transaction.
	// All changes made by the transaction become visible to others and are guaranteed to be durable if a crash occurs.
	CommitTx(context.Context) error
	// RollbackTx rolls back the current transaction and causes all the updates made by the transaction to be discarded.
	RollbackTx(context.Context) error
}

type TwoPhaseCommitProtocol

type TwoPhaseCommitProtocol interface {
	OnePhaseCommitProtocol
	// PrepareTx communicate with the resource that the current transaction is done and should be prepared for commit later.
	//
	// Prepare transaction is not intended for use in applications or interactive sessions.
	// Its purpose is to allow an external transaction manager to perform atomic global transactions across multiple databases or other transactional resources.
	// Unless you're writing a transaction manager, you probably shouldn't be using PrepareTx.
	//
	// This command must be used on a context made with BeginTx.
	// Calling CommitTx or RollbackTx with the received context
	// must be interpreted as Two Phase Commit Protocol's Commit or Rollback action.
	PrepareTx(context.Context) (context.Context, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL