Reputation: 41
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
Upvotes: 1
Views: 7836
Reputation: 111
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:
@Resource
SessionContext context
then use the context reference to retrieve the current UserTranaction inside the business method
context.getUserTransaction();
Upvotes: 0
Reputation: 33946
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:
Upvotes: 1