既存アプリはデータベースへ更新機能をもっていまして、WEBAPI追加実装の場合、既存アプリ側で行う更新とWEBAPIによって呼ばれて実行された更新などでほぼ同タイミングで行われ、競合が起きないようにするにはどうしたらよいのでしょうか。
既存アプリ
クライアント側 → 更新 → データベース
追加プロジェクト
WEBAPI → 更新 → データベース
実装資料など今いろいろ見ていますが、もともとのメソッドを囲い込むようにWEBAPIの実装を行う記述がなく、(新規実装を例にしているためだと思いますが)、別にWEBAPI側として検索更新メソッドを準備しているように見えました。
それとも既存アプリに対して新規でWEBAPI用のクラスを作り待ち受けるのではなく、まったく新規に作り変えていかなければいけないということなのでしょうか。
トランザクションの処理など入れて競合が起きないような実装をするには、どのような記述を行っていったらよいのでしょうか。
例として環境面は以下になります。
既存アプリ
windows7
VB.NET sqlSerever
.NET Framework 4.6
WebAPI ASP.NET
既存アプリの処理
~一部省略~ SqlConnectionStringBuilder dbConString = new SqlConnectionStringBuilder(); dbConString.UserID = "●●●●●●"; dbConString.Password = "●●●●●●●"; dbConString.DataSource = "192.168.●●●.●●●,●●●●"; dbConString.InitialCatalog = "TestDB"; sql = "UPDATE USER_MASTER SET DEPT_NO = '0004' WHERE USER_ID = '0001' "; SqlConnection con = new SqlConnection(dbConString.ConnectionString); try { con.Open(); SqlCommand command = new SqlCommand(sql, con); sqlReader = command.ExecuteReader(); ~一部省略~
追加プロジェクト
public class CustomersController : ApiController { public HttpResponseMessage PutCustomer(int id,Customer customer) { ~一部省略~ SqlConnectionStringBuilder dbConString = new SqlConnectionStringBuilder(); dbConString.UserID = "●●●●●●"; dbConString.Password = "●●●●●●●"; dbConString.DataSource = "192.168.●●●.●●●,●●●●"; dbConString.InitialCatalog = "TestDB"; sql = "UPDATE USER_MASTER SET DEPT_NO = customer WHERE USER_ID = id "; SqlConnection con = new SqlConnection(dbConString.ConnectionString); con.Open(); // Start a local transaction. SqlTransaction sqlTran = connection.BeginTransaction(); // Enlist a command in the current transaction. SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; try { SqlCommand command = new SqlCommand(sql, con); sqlReader = command.ExecuteReader(); }catch (Exception ex) { // Handle the exception if the transaction fails to commit. Console.WriteLine(ex.Message); try { // Attempt to roll back the transaction. sqlTran.Rollback(); } catch (Exception exRollback) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. Console.WriteLine(exRollback.Message); } } ~一部省略~ return Request.CreateResponse(HttpStatusCode,OK); } }
以下実行
http://~/api/customers/?id=0001&customer=0004
そもそも基本的なことに立ち返りますが、既存側と追加WEBAPIプロジェクト側でトランザクション処理を行っていさえすれば、競合起こった場合などしっかりロールバックを行い、心配いらないということなのでしょうか。
もしそうなのでしたらお恥ずかしい限りですが、よろしくお願い致します。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/19 01:18
退会済みユーザー
2017/11/19 01:48
2017/11/19 02:37
退会済みユーザー
2017/11/19 03:11 編集
2017/11/19 05:15
退会済みユーザー
2017/11/20 00:50 編集