かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[NetBeans][Java]EJB3でのトランザクション その1

EJB3でセッションビーンが簡単に作れることがわかった!
ということで、今度はトランザクションの動きについてみてみる。
とりあえず、何も考えずに素のトランザクションの動きは、何もおきずに正常終了するとコミット。検査例外もコミット。非検査例外はロールバック。という動きになるみたい。

とりあえず、NetBeansにデフォルトでついてくるSampleデータベースのCustomerテーブルに更新をかけるセッションビーンを作ってみた。

@Stateless
public class TransactionTestBean implements TransactionTestLocal {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    public void update(int id, String name, boolean isThrow) {
        Customer customer = entityManager.find(Customer.class, id);
        if (customer != null) {
            // customerのnameを更新
            customer.setName(name);
        }
        if (isThrow) {
            // 第三引数がtrueの場合は例外発生
            throw new RuntimeException("エラーじゃ");
        }
    }
}

第三引数がtrueだと例外が飛ぶ仕組み。
これを呼ぶための画面を適当にこさえて実験!!
image

textFieldIDのvalueをcusomerIDプロパティにバインドして、textFieldNameをcustomerNameプロパティにバインドしています。ボタンは、更新がbutton1で更新(失敗)がbutton2です。
各々のボタンのアクションに下のようなコードを書いてみた。

    @EJB
    private TransactionTestLocal transactionTest;

    public String button1_action() {
        transactionTest.update(customerId, customerName, false);
        info("更新成功");
        return null;
    }

    public String button2_action() {
        try {
            transactionTest.update(customerId, customerName, true);
        } catch (EJBException ex) {
            error(ex.getMessage());
        }
        return null;
    }

実行前にCUSTOMERテーブルの中身をチェック!
image

ID:1の人はJumboComさん。メモメモ。ということで、実行!
image

まずは、更新ボタンを押してみる。
image

JumboComさんの名前が更新されてる。次は、更新(失敗)を押してみる。
image(下の更新成功メッセージは、更新ボタンを押したときのもの)

ぽちっとな。
 image

ちゃんと更新されずにロールバックされている。
何も考えなくてもトランザクションの面倒を見てくれてる。

設計するときは、基本的にこの形にはまるように設計すると楽が出来そうだ。

投稿日時 : 2008年6月9日 7:58

Feedback

# nike air max 95 2012/12/07 16:55 http://superairmaxshoes.webs.com/

Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more from this post. I'm very glad to see such magnificent info being shared freely out there.

# Christian Louboutin Pumps 2012/12/08 13:47 http://myclsale1.webs.com/

I got what you mean , thankyou for posting .Woh I am happy to find this website through google. "Money is the most egalitarian force in society. It confers power on whoever holds it." by Roger Starr.

# longchamp pliage messager 2012/12/14 20:46 http://www.sacslongchamp2012.info/sacs-longchamps-

I realize the pricey garbage thought. I can't stand the search, sound and feel of your Beats.

# burberry uk 2012/12/15 23:06 http://www.burberryuksale.info/category/handbags-b

Keep the excellent deliver the results.

# sac longchamp pas cher 2012/12/16 18:00 http://www.saclongchampachete.info/category/sac-lo

My organization is impressed by the grade of information on that website. There are a great deal of good solutions here.

# michael kors sac a main 2012/12/18 2:15 http://michael-kors-canada.webnode.fr/blog/

Ill be down again the track to check out other content that.

タイトル
名前
Url
コメント