BMT EJB 如何使用 UserTransaction
我正在尝试创建 BMT EJB 的示例并手动控制事务,但是当尝试运行时出现 NullException,并且我不知道我做错了什么。 下面是代码:
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@Local(GlobalTLocal.class)
public class GlobalT implements GlobalTLocal {
@Resource
private UserTransaction utx;
public GlobalT() {
}
@Override
public void sincroniza() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sqlPostgres = "SELECT * FROM RH.RHINFORMIX";
String insert = "INSERT INTO RH.RHINFORMIX (id_agente,fk_codigo_area,nome,email,excluido,tipoinclusao,id,teste)"+
" VALUES (11,11,'teste','teste',false,'I',12,'teste')";
InitialContext ic = new InitialContext(env);
DataSource ds = (DataSource) ic.lookup("jdbc/RHMigracaoPostgres");
con = ds.getConnection();
utx.begin();
// con.setAutoCommit(false);
stmt = con.createStatement();
rs = stmt.executeQuery(sqlPostgres);
while (rs.next()) {
System.out.println("Query '" + sqlPostgres + "' returned "
+ rs.getString(3));
}
stmt.executeUpdate(insert);
// con.commit();
// if(true){
// throw new Exception();
// }
utx.commit();
} catch (Exception e) {
try {
utx.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally{
if(rs != null)
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
当我运行这个类时,我收到以下错误:
java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:94)
at Principal.main(Principal.java:11)
java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:71)
at Principal.main(Principal.java:11)
This EJB isDeployed at a Jboss 4.2.2serve 并且在 Jboss 启动期间没有错误,我们可以看到部署的 EJB 的日志:
15:51:33,529 INFO [EJB3Deployer] Deployed: file:/C:/Jboss-Limpos/jboss-4.2.2.GA/server/all/deploy/GlobalTransaction.jar
I'm试图强制 UserTransaction 为我提交插入!
我做错了什么?
塔克斯
I'm trying to create an example of BMT EJB and control the transaction manualy but when try to run I got an NullException and I don't kno what I'm doing wrong.
Here is the code:
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@Local(GlobalTLocal.class)
public class GlobalT implements GlobalTLocal {
@Resource
private UserTransaction utx;
public GlobalT() {
}
@Override
public void sincroniza() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sqlPostgres = "SELECT * FROM RH.RHINFORMIX";
String insert = "INSERT INTO RH.RHINFORMIX (id_agente,fk_codigo_area,nome,email,excluido,tipoinclusao,id,teste)"+
" VALUES (11,11,'teste','teste',false,'I',12,'teste')";
InitialContext ic = new InitialContext(env);
DataSource ds = (DataSource) ic.lookup("jdbc/RHMigracaoPostgres");
con = ds.getConnection();
utx.begin();
// con.setAutoCommit(false);
stmt = con.createStatement();
rs = stmt.executeQuery(sqlPostgres);
while (rs.next()) {
System.out.println("Query '" + sqlPostgres + "' returned "
+ rs.getString(3));
}
stmt.executeUpdate(insert);
// con.commit();
// if(true){
// throw new Exception();
// }
utx.commit();
} catch (Exception e) {
try {
utx.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally{
if(rs != null)
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
When I run this class I receive the following error:
java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:94)
at Principal.main(Principal.java:11)
java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:71)
at Principal.main(Principal.java:11)
This EJB is deployed at a Jboss 4.2.2 serve and there is no erro during the Jboss startup, and we can see log of the deployed EJB:
15:51:33,529 INFO [EJB3Deployer] Deployed: file:/C:/Jboss-Limpos/jboss-4.2.2.GA/server/all/deploy/GlobalTransaction.jar
I'm trying to force that the UserTransaction commit the insert for me!!!
What am I doing wrong?
Tks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用主类,则必须使用应用程序客户端容器,并且您的 @Resource 必须是主类中的静态字段,而不是您创建的对象中的静态字段。但是,请注意,JavaEE 平台规范并不要求应用程序客户端容器支持 UserTransaction。如果 JBoss 在其应用程序客户端容器中不支持 UserTransaction,那么您的选择是:
If you are using a main class, then you must launch using an application client container, and your @Resource must be a static field in the main class, not in an object you create. However, note that the JavaEE platform specification does not require application client containers to support UserTransaction. If JBoss does not support UserTransaction in its application client container, then your options are:
我相信您应该避免使用 @Resource 在 Bean 级别(BMT 无状态 bean)检索 UserTransaction。相反,您可以使用以下方式获取对 SessionContext 的引用:
然后使用上下文引用来检索业务方法内的当前 UserTranaction
I believe you should avoid using @Resource for retrieving UserTransaction at Bean level (BMT stateless bean). Instead, you can get the reference to SessionContext using:
then use the context reference to retrieve the current UserTranaction inside the business method