> 8页)
_0_我的一共就205页
_0_我的一共就205页
我前段时间刚在JBOSS配置过XA DataSource。中间是很多环节都容易出错。主要原因不是Application Server的问题,而是像Oracle这样支持分布式数据库的问题。按网上Oralce的安装攻略,安装好的Oracle,其实根本还不支持分布式事务,需要另外做一些初始化的工作。
引用《EJB设计模式》里的原话,第228页
Always Call setRollbackOnly when
Application Exceptions Occur
An important but unemphasized fact is that application exceptions(developer
written exceptions) thrown from an EJB to client don't trigger automatic
rollbacks of the running transaction,in contrast to EJBExceptions,which auto-
matically trigger the current transaction to roll back.Serious data consistency
problems can arise if a use case fails without the transaction rolling back.
Therefore,always remember to first catch application exceptions and call
ctx.setRollbackOnly() (where ctx is of type javax.ejb.SessionContext
for session beans)before rethrowing or wrapping application exceptions to the
client.
我同意有位道友的观点,遇到运行时异常(RuntimeException)及其子类,容器管理的Bean才回滚。
在文中提到的EJBException是RuntimeException的子类。而SQLException,NamingException的父类是Exception。个人感觉就《EJB设计模式》中所说的application exception概念有的笼统,按习惯性的思维SQLException应该不会包括在里面。
哎,可是事实胜于雄辩。
我们都知道,struts实现了MVC中的V和C,对M没有实现,我们需要写一些业务代理(普通java对象)来实现M,所谓的业务代理只不过是隐藏了对SLSB的查找逻辑和一些异常的包装;SLSB封装了业务逻辑,它通过JNDI获取数据源,然后取得连接,然后用数据库连接构造若干DAO并调用起方法,对事物进行管理(例如当DAO抛出SQLException时调用ctx.setRollbackOnly() ,这样保证了数据的完整性)
banq还真顽固,不懂装懂呵呵
然后程序这样调用:
InitialContext ctx = new InitialContext();
Object txObj = ctx.lookup("java:/MSSQLXADS");
UserTransaction utx = (UserTransaction) txObj;
utx.begin();
Context initCtx = new InitialContext();
javax.sql.DataSource ds =( javax.sql.DataSource)initCtx.lookup("java:/MSSQLXADS");
Connection conn = ds.getConnection();
PreparedStatement pstm= conn.prepareStatement("insert into dstest values (?)");
pstm.setString(1,"begin");
pstm.executeUpdate();
utx.commit();
结果报错:javax.naming.NameNotFoundException: MSSQLXADS not bound :(
请问还需要在哪里配置数据源么? 或者是我的写法有问题? 不胜感激!