Unable to connect to mysql db with JPA (java persistance) - java

I'm fairly new to Java--though, coming from PHP + JS. I aim to make a CRUD (+MVC) with Java Servlets.
I am using Java EE 7. I based some of my patterns here with this and that (both have the same final files).
So, I am implementing MVC, and I can't seem to make my code to work. JPA seems to be the main issue; I can't fetch anything from the database.
Here's the repository (really small project -- 7~8 classes). Models; DAO.
I had setup controllers.Test(url: /test) to test if I could actually communicate with the database via JPA. However, when I go to /test(controllers.Test), an exception is thrown. The same goes for controllers.NewsEdit(url: /edit).
java.lang.NullPointerException
controllers.Test.doGet(Test.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
When I visit home(url: /; controller.NewsIndex), I receive no exceptions -- I get the homepage, but no data at all. Is there anything I'm doing wrong?
My SQL files are located in the /sql folder. I got persistent.xml on META-INF/.
Any help or reference would be appreciated. Really need to learn it..
Thanks!

Ok,
A lil explanation about NullPointerException.
When the variable (in this case private NewsDAO newsDAO;) hasn't been initialized, that "pointer" is pointing to nothing in memory. Then when you try to call a method, there is nothing there and the NPE happens.
The problem here is that private NewsDAO newsDAO; is not being injected by the container and therefore when you call newsDAO.all(); newDAO is null and throws the exception.
CDI Injection only happens for components managed by the containers. I'm a spring guy so I'm not sure where to go here. Try to figure out if your NewsDAO is being loaded by the container.
Tomcat is a web container... You need a full Application server with an EJB container to run your example. Try to download and run on glassfish, JBoss or any other full app server.
more info here.
http://en.wikipedia.org/wiki/Application_server#Java_application_servers
and
http://www.oracle.com/technetwork/java/javaee/overview/compatibility-jsp-136984.html

Related

javax.ejb.EJBException: Failed to read response, in wildfly

I have this Exception, please I need your support.
there are one wildfly20 instance to services and other wildfly20 instance to tier web.
Web tier consume jdni as service in remote enviroment. It's testing and work well with some methods.
Now, right here come the case. Some methods from jndi tier work without problem and web call it, and get resources well, But, some others when Web tier call jndi call receive this exception (javax.ejb.EJBException: Failed to read response).
Please, Let's me know witch could be the cause for this exception triggered?.
Any hints that help with that.
every one.
I'm answer my own quetion for anyone with same issue, because I dont find information that I could use to resolve it.
Like I said I have two instance of wildfly.
1 web tier project.
2 JNDI tier with services.
In the web tier just add in the jboss-deployment-struture.xml hibernate dependency module. It's default wildfly in modoles set org.hibernate.
That's it.

Transaction Propagation issue

We recently had a very unexpected issue in PROD which we manage to reproduce but we still don't have a clear explanation of what is causing it and more importantly how to fix it.
Our system is a vendor workflow framework that offers us hooks to add our custom behavior. It is running on WebSphere Application Server and it is a mix of Camel and Spring and EJB. Initially it was pure EJB but a few years ago the vendor added Camel with a view of getting rid of application server.
Now the problem:
The start of processing is a Camel route which is transacted with PROPAGATION_REQUIRED attribute. Part of the processing there is a local call to an EJB method. There is not explicit setting for any of EJB methods exposed apart from being Container managed. This means the EJB call should get the implicit value of REQUIRED. Part of the EJB call there is data change happening. The reference to the EJB is obtained using some code like below:
String repositoryName = MBLLookupUtil.getInstance().getRepositoryName();
RepositoryInstanceFacadeHome facadeHome = MBLLookupUtil.getInstance().getRepositoryInstanceFacadeHome(
repositoryName);
RepositoryInstanceFacade facade = facadeHome.create();
// Here is when the data change happens:
facade.doSomeWork();
The application logs show clearly that the EJB processing is happening in the same thread as the client. After making the call there is more work in the calling client when a Timeout exception happens.
We expect that both data change on client side and that changed as part of the EJB call will be roll backed, however the data changed by EJB call stays committed while the client side (Camel) gets roll backed.
In the spring application context we use WebSphereUowTransactionManager which is the IBM recommended way of configuring transaction manager:
<bean id="txManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
Any hints about what could cause this or how we could further investigate would be great appreciated. Thank you in advance.
UPDATE:
As advised in tomgeraghty3's comment I made a code change to log the value of TransactionSynchronizationManager.isActualTransactionActive() and the result was true
UPDATE 2:
We found out where the issue was by spending hours of analyzing decompiled vendor code. The transaction propagation was configured in an application-context.xml file but it was also overwritten straight in the POJO code with an #Transactional(propagation=REQUIRED_NEW) annotation. We proved that was the problem by removing the POJO class from the vendor jar and adding one with no annotation with the same package in one of ours. Very dirty just to prove our suspicion.
Now our bigger challenge would be to persuade the vendor address the issue. Hope it is just a bug and not an impacting change.

Vaadin and JPARepository: transaction errors

I'm having troubles in integrating Vaadin and spring-data (JPARepositories in particular).
After following the guidance of Vaadin's team with their last webinar I managed to configure my application using Spring starter (spring boot), adding Vaadin, JPA and a Postgresql database. Using this method was really straightforward and loading entities to a table works out of the box by simply having this
#Autowired private ProjectDAO projects;
// other code here ...
grid.setContainerDataSource(new BeanItemContainer<Project>(Project.class, projects.findAll()));
My repository is just
#Repository
public class ProjectDAO implements JPARepository<Project, Long> {
}
And, as I said, this works flawlessly. The problem is when I try to save something: when trying to save a project via a button click, for example
btnSave.addClickListener(e -> saveCurrent());
private void saveCurrent() {
// ... here I do some filesystem operations, nothing that requires DB connection
setModified();
}
public void setModified() {
project.setLastAccess(new Date());
projects.saveAndFlush(project);
}
I get an exception:
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress.
I've tried many different things, such as marking the setModified method as #Transactional, or encapsulating the setModified into a service class, but everything always throws this exception.
I find it rather strange that this doesn't work "out of the box", and I'd rather not work with transactions myself, since there are already the instruments to do that.
Any help would be really appreciated
EDIT 12/05/2015
It seems that this problem only appears when using an external server, not the embedded one provided by spring-boot. For test purposes I can manage to use the built-in, but eventually I will have to deploy the application on an existing server, so what could the issue be? Tomcat's configuration?
If anyone needs the answer I'll leave here what I found out after a few days of breaking my head on it.
Before deploying the application I changed some configurations on the main class that was generated by Spring Initializr (The application one) and made it extend SpringBootServletInitializer because I was following the guide I found in the official spring blog. Turns out that this was in fact not necessary because I set up "WAR" in Spring Initializr from the beginning, thus the application already had a class extending that one, and as a result when deploying it on Tomcat, the web application started twice, so the application was configured twice and two persistence units were instantiated. This probably was the error, because after reverting the changes I made to the application everything worked fine.
TL;DR: if you use Spring Initializr to create a WAR application don't touch anything.

How do I test if EJB is running on JBoss AS 7 server

Again, many excellent answers from the fine folks here on SO. I am still unable to connect. But I believe Gimby might have had a good suggestion that there may be something wrong with the server or the beans. According to the log file generated when JBoss started, my beans have been deployed. The administrator of the server is unable to run the admin console for vulnerabilities reasons so there is no way to see if the beans are running. Is there a command line tool that I could point him to for testing? Is there a simple test I could write that would check the beans? I have tried most everything I have found and others have suggested and keep getting various errors. most times being:
javax.naming.CommunicationException: Could not obtain connection to any of these urls: remote://:4647
I've encountered other errors as I have made changes to various files and code but this one is the most frequent. If the call I make to the bean is right, and that is questionable, then how do I tell if the bean is even running or not>
There seems to be some uncleared situations here.
Still I want to ask you, if your bean got a remote interface.
You could use lookup to find your bean on this server.
You also could deploy for example a RESTservice on your jboss server, which look for your bean locally, so you dont have to specify any connection properties, but you would need the jndi name.
Hope this helps a bit, greets Jerome.

How do I fix this JBoss EJB client authentication issue?

I have an EJB deployed under JBoss (we're moving a project to it from Weblogic), I can get an EJBHome reference to the EJB via a JNDI lookup.
The login-config.xml application-policy contains several login modules (I don't know if these are ANDs or ORs?) - org.jboss.security.spi.BaseCertLoginModule, LdapLoginModule, RoleMappingLoginModule and ClientLoginModule.
When my client code tries to invoke the create method via reflection, calling invoke, I get an InvocationException which wraps an AccessException, which wraps a final exception of javax.security.auth.login.FailedLoginException: Password Incorrect/Password Required at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:213)....
Am I missing some client code to use BaseCertLoginModule, or one of the others in the login-config.xml, instead of UsernamePasswordLoginModule?
Thanks in advance, any suggestions appreciated, apologies for not posting the entire stacktrace but it's on a secured network without internet access.

Categories

Resources