▁ ▁The ▁code ▁sample ▁below ▁illustr ates ▁the ▁usage ▁of ▁User Transaction ▁via ▁bean - managed ▁transactions ▁in ▁an ▁E JB ▁session ▁bean : ▁ ▁@ Stat eless ▁@ Transaction Management ( BE AN ) ▁public ▁class ▁Example Bean ▁{ ▁▁▁▁▁ ▁@ Resource ▁▁▁▁ ▁private ▁User Transaction ▁ut x ; ▁▁▁▁▁ ▁public ▁void ▁foo () ▁{ ▁▁▁▁▁▁▁▁ ▁// ▁start ▁a ▁transaction ▁▁▁▁▁▁▁▁ ▁ut x . begin (); ▁▁▁▁▁▁▁▁▁ ▁// ▁Do ▁work ▁▁▁▁▁▁▁▁▁ ▁// ▁Comm it ▁it ▁▁▁▁▁▁▁▁ ▁ut x . commit (); ▁▁▁▁ ▁} ▁} ▁ ▁Alternatively , ▁the ▁User Transaction ▁can ▁be ▁obtained ▁from ▁the ▁Session Context : ▁ ▁@ Stat eless ▁@ Transaction Management ( BE AN ) ▁public ▁class ▁Example Bean ▁{ ▁▁▁▁▁ ▁@ Resource ▁▁▁▁ ▁private ▁Session Context ▁ctx ; ▁▁▁▁▁ ▁public ▁void ▁foo () ▁{ ▁▁▁▁▁▁▁▁ ▁User Transaction ▁ut x ▁= ▁ctx . get User Transaction (); ▁▁▁▁▁▁▁▁▁ ▁// ▁start ▁a ▁transaction ▁▁▁▁▁▁▁▁ ▁ut x . begin (); ▁▁▁▁▁▁▁▁▁ ▁// ▁Do ▁work ▁▁▁▁▁▁▁▁▁ ▁// ▁Comm it ▁it ▁▁▁▁▁▁▁▁ ▁ut x . commit (); ▁▁▁▁ ▁} ▁} ▁ ▁Note ▁though ▁that ▁in ▁the ▁example ▁above ▁if ▁the ▁@ Transaction Management ( BE AN ) ▁annotation ▁is ▁omitted , ▁a ▁J TA ▁transaction ▁is ▁automatically ▁started ▁whenever ▁foo () ▁is ▁called ▁and ▁is ▁automatically ▁committed ▁or ▁rolled ▁back ▁when ▁foo () ▁is ▁ex ited . ▁M aking ▁use ▁of ▁a ▁User Transaction ▁is ▁thus ▁not ▁necessary ▁in ▁E JB ▁programming , ▁but ▁might ▁be ▁needed ▁for ▁very ▁special ized ▁code . ▁ ▁User Transaction ▁support ▁in ▁J ND I ▁The