|
事物能够保证我们的业务完整正确,mdb本身也提供事物机制。今天尝试了下JTA事物。
private Context createContextInstance(String jndi_factory, String url,
String user, String pwd)
{
Context ic = null;
try
{
Hashtable env = new Hashtable();
if (MessageUtil.hasContent(jndi_factory))
env.put(Context.INITIAL_CONTEXT_FACTORY, jndi_factory);
else
env.put(Context.INITIAL_CONTEXT_FACTORY, s_JNDI_FACTORY);
if (MessageUtil.hasContent(url))
env.put(Context.PROVIDER_URL, url);
else
env.put(Context.PROVIDER_URL, s_url);
if (MessageUtil.hasContent(user))
env.put(Context.SECURITY_PRINCIPAL, user);
if (MessageUtil.hasContent(pwd))
env.put(Context.SECURITY_CREDENTIALS, pwd);
ic = new InitialContext(env);
if (MessageUtil.hasContent(user))
ic.addToEnvironment(Context.SECURITY_PRINCIPAL, user);
if (MessageUtil.hasContent(pwd))
ic.addToEnvironment(Context.SECURITY_CREDENTIALS, pwd);
} catch (Exception e)
{
ic = null;
e.printStackTrace();
s_logFile.error("Cann't create the conetxt!!", e);
}
return ic;
}
Context ctx = createContextInstance();
UserTransaction tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");
tx.begin();
try
{
........
tx.commit();
}catch()
{
tx.rollback();
} |
|
|