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

0

Situation:

I have an EJB Project. It has a Class model that contains the business logic and database connection (the 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 their remote access interfaces.

The project deploys WildFly correctly. From the EJB Project, the BBDD persists correctly.

Client:

It's a Java Project. The problem appears when the client wants to use the EJBs.

I have everything well configured: the jndi.properties, the BuildPath, the WildFLy libraries, etc. The doubt and the error, I think, comes from the specifications of the EJBs that I can not find anywhere.

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

If it is possible that the EJBs can use them, I do not understand why the remote client can not use the functionalities.

I hope I have been clear. I do not attach code because all the logic code is tested and works well. The specific doubt is highlighted in bold.

    
asked by tincho hernández 09.07.2016 в 02:26
source

2 answers

0

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

Now you have two options within an EJB to handle transactions:

  • Managed by Bean: "@TransactionManagement (TransactionManagementType.BEAN)", which means that the connection is removed from the container by EntityManager (with JPA).
  • Managing Managed by contender: "@TransactionManagement (TransactionManagementType.CONTAINER)", the connection is created using UserTransaction using JDBC (without JPA).

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

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

    
answered by 12.07.2016 в 17:12
0

Good morning

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

Answer: If an EJB can use a class that is connected to a db, which is configured for JPA as for JDBC.The correct form should 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 (business layer) itself has to implement this 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 a sample context statement for a remote client and a link for additional information:

context jboos:

    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: link

    
answered by 20.11.2017 в 15:43