Can a remote EJB use classes (which are not EJBs) to be used by a remote client?

Situation:

I have an EJB Project. It has a Class model that contains business logic and database connection (classes use JDBC). Within the same project, these classes are used by remote EJBs. Why? Because these EJBs will be used by a remote client, through its remote access Interfaces.

The project deploys to WildFly correctly. From the EJB Project is persisted in the BBDD correctly.

Client:

Is a Java Project. The problem appears when the customer wants to use the EJBs.

I have everything well set up: the jndi.properties, the BuildPath, WildFLy libraries, etc. the doubt and error, I think, comes from the hand of EJBs specifications that I can't find anywhere.

The doubt is: can an EJB use classes that connect to the BBDD via JDBC (PreparedStatement, ResultSet, etc.), or should I Yes or yes use JPA?

If it is possible that EJBs can use them, I don't understand why the remote client can't use the functionalities.

I hope I was clear. I don't attach code because all the logic code is tested and works fine. The point doubt is highlighted in bold.

 0
Author: tincho hernández, 2016-07-09

2 answers

If it is an EJB it should be at least annotated with @Stateless or @Stateful, which means it is EJB. Now these EJBs are managed by Wildfly inside a container, one of these management is the transactions.

You now have two options inside an EJB to handle transactions:

  • managed by Bean: "@TransactionManagement (TransactionManagementType.BEAN)", which means you pull the connection out of the container using EntityManager (with JPA).
  • managing handled by container: "@TransactionManagement (TransactionManagementType.CONTAINER)", the connection of the creas using UserTransaction using then JDBC (without JPA).

In both cases the connections are taken from a DataSource in your standalone configuration.XML.

Now you can also create connections inside the EJB manually: from creating the DataSource and the entire cycle to performing a query and closing the connection. Everyone would be under your Control and not Wildfly.

 0
Author: Willy, 2016-07-12 15:12:53

Good

The question is: Can an EJB use classes that connect to the BBDD using JDBC (PreparedStatement, ResultSet, etc.), or should I Yes or yes use JPA?

Answer: if an EJB I can use a class that connects to a db, which is configured for JPA as for JDBC.La correct way would have to be to call from the client to the logic class which makes use of the data access layer. Via @Remote with an interface declared in the logic layer (layer business) itself has to implement such an interface.

The client has via JNDI with:

private static final String JNDI_EJB_REMOTE= "PROYECTO_NAME/CLASE_EJB/REMOTE_OR_EJBNAME";

Mibeanremoteinterface bean = (MiBeanRemoteInterface) context.lookup (JNDI_EJB_REMOTE);

I leave you an example context Statement for a remote client and a link for additional information:

Jboos Context:

    Properties properties = new Properties();
    properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
    properties.put("java.naming.provider.url", "jnp://localhost:1099");
    Context context = new InitialContext(properties);

Link: https://docs.jboss.org/author/display/WFLY8/Remote + JNDI + Reference? _sscc=t

 0
Author: Angel Brun, 2017-11-20 14:43:54