Skip to content

Commit

Permalink
Added initial timeout handling. paired: Jae
Browse files Browse the repository at this point in the history
  • Loading branch information
toddharding committed Mar 10, 2017
1 parent 8c4ee39 commit 092d977
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/mssqlex/odbc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ defmodule Mssqlex.ODBC do
| {:updated, non_neg_integer()}}
| {:error, Exception.t}
def query(pid, statement, params) do
GenServer.call(pid,
{:query, %{statement: IO.iodata_to_binary(statement), params: params}})
if Process.alive?(pid) do
GenServer.call(pid,
{:query, %{statement: IO.iodata_to_binary(statement), params: params}})
else
{:error, :no_connection}
end
end

@doc """
Expand All @@ -58,7 +62,11 @@ defmodule Mssqlex.ODBC do
"""
@spec commit(pid()) :: :ok | {:error, Exception.t}
def commit(pid) do
GenServer.call(pid, :commit)
if Process.alive?(pid) do
GenServer.call(pid, :commit)
else
{:error, %Mssqlex.Error{message: :no_connection} }
end
end

@doc """
Expand All @@ -68,7 +76,11 @@ defmodule Mssqlex.ODBC do
"""
@spec rollback(pid()) :: :ok | {:error, Exception.t}
def rollback(pid) do
GenServer.call(pid, :rollback)
if Process.alive?(pid) do
GenServer.call(pid, :rollback)
else
{:error, %Mssqlex.Error{message: :no_connection} }
end
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Mssqlex.Mixfile do

def project do
[app: :mssqlex,
version: "0.4.1",
version: "0.4.2",
description: "Adapter to Microsoft SQL Server. Using DBConnection and ODBC.",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
Expand Down
8 changes: 8 additions & 0 deletions test/mssqlex/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ defmodule Mssqlex.TransactionTest do
Mssqlex.query(pid, "SELECT * from #{table_name};", [])
end

test "failing transaction timeout test", %{pid: pid} do
assert_raise Mssqlex.Error, fn ->
DBConnection.transaction(pid, fn _ ->
:timer.sleep(1000)
end, [timeout: 0])
end
end

test "manual rollback transaction test", %{pid: pid} do
table_name = "transaction_test.dbo.roll_back"
assert {:error, :rollback} =
Expand Down

0 comments on commit 092d977

Please sign in to comment.