操作两个数据库出错,大家给指点一下!!!!

shwwwx 2007-12-10 02:45:41
在JTA中操作两个数据库出错,不知什么原因,在网上也没查到。
javax.transaction.UserTransaction tx = null;
java.sql.Connection conn = null;
java.sql.Connection conn2 = null;
String sql = "select * from blog";
String sql2 = "select * from person";
try {
Context context = new InitialContext();
tx = (UserTransaction) context
.lookup("java:comp/UserTransaction"); // 取得JTA事务,本例中是由Jboss容器管理
javax.sql.DataSource ds = (javax.sql.DataSource) context
.lookup("java:/PostgresDS"); // 取得数据库连接池,必须有支持XA的数据库、驱动程序
javax.sql.DataSource ds2 = (javax.sql.DataSource) context
.lookup("java:/PostgresDS2"); // 取得数据库连接池,必须有支持XA的数据库、驱动程序

tx.begin();
conn = ds.getConnection();
//conn = getJtaConnection("java:/PostgresDS2");
java.sql.Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
System.out.println("***************:" + rs.getString(2));
}
rs.close();
statement.close();
conn.close();
ds = null;
conn2 = ds2.getConnection(); //(这里出错)
java.sql.Statement statement2 = conn2.createStatement();
ResultSet rs2 = statement2.executeQuery(sql2);
while (rs2.next()) {
System.out.println("person is :" + rs2.getString(2));
}
statement2.close();
rs2.close();
conn2.close();
tx.commit();
难道在一个事务里,不能用两个Connection?
...全文
237 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
帖子就这样沉了!
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
谁来帮忙指点一下!
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
网上说这方面的好像不多
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
这个异常好像是说,在事务里,创建第二个数据源出错,是不是要在什么地方设置一下才行。
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
加上prop错误还是这样:
org.jboss.util.NestedSQLException: Could not enlist in transaction on entering meta-awareobject!;
- nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f570278:702:475de3c1:3f status: ActionStatus.ABORT_ONLY >);
- nested throwable: (org.jboss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!;
- nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f570278:702:475de3c1:3f status: ActionStatus.ABORT_ONLY >))
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:94)
rabbitbug 2007-12-11
  • 打赏
  • 举报
回复
代码固然重要
但错误信息更重要
rabbitbug 2007-12-11
  • 打赏
  • 举报
回复
你连什么错都不贴出来
让大家怎么判断啊
bloodrate 2007-12-11
  • 打赏
  • 举报
回复
一个connection怎么操作两个数据库??我还真不知道
zlheadof 2007-12-11
  • 打赏
  • 举报
回复
一个connction就可以吧,为什么要用两个呢?
shwwwx 2007-12-11
  • 打赏
  • 举报
回复
都一个星期了,哪位高手能帮帮小弟啊!
天外流星 2007-12-10
  • 打赏
  • 举报
回复
可以试下如下操作获取Context


Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
prop.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

shwwwx 2007-12-10
  • 打赏
  • 举报
回复
我把 tx = (UserTransaction) context.lookup("java:comp/UserTransaction");
换成 tx=(UserTransaction)context.lookup("java:/UserTransaction");
报错:
javax.naming.NameNotFoundException: UserTransaction not bound
这样好像找不到 UserTransaction ?
shwwwx 2007-12-10
  • 打赏
  • 举报
回复
谢谢,我试试!
天外流星 2007-12-10
  • 打赏
  • 举报
回复
上面更正:


你使用的JNDI name是tomcat的

而在JBOSS下应该是



tx=(UserTransaction)context.lookup("java:/UserTransaction");




天外流星 2007-12-10
  • 打赏
  • 举报
回复
你使用的JNDI name是tomcat的

而在JBOSS下应该是


tx=(UserTransaction)context.lookup("java:comp/UserTransaction");

天外流星 2007-12-10
  • 打赏
  • 举报
回复
http://docs.jboss.org/jbossas/admindevel326/html/ch4.chapt.html
天外流星 2007-12-10
  • 打赏
  • 举报
回复
JBoss直接从JNDI中获取TransactionManager
ctx.lookup("java:/TransactionManager");
shwwwx 2007-12-10
  • 打赏
  • 举报
回复
这个是我的配置:postgres-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>PostgresDS</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/jta1</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>postgres</user-name>
<password>wwx1226</password>
<idle-timeout-minutes>50</idle-timeout-minutes>

<metadata>
<type-mapping>PostgreSQL 8.2-506</type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>PostgresDS2</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/jta2</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>postgres</user-name>
<password>wwx1226</password>
<idle-timeout-minutes>50</idle-timeout-minutes>
<metadata>
<type-mapping>PostgreSQL 8.2-506</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
shwwwx 2007-12-10
  • 打赏
  • 举报
回复
我用的是pgsql数据库,在postgres-ds.xml里配置了两个数据源!
shwwwx 2007-12-10
  • 打赏
  • 举报
回复
这个问题都困扰我三天了,在网上也没找到答案!
加载更多回复(7)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧