2

I'm trying to create a GWT app and I'm working with a local postgreSQL Database. I'm working with GWT 2.4 on eclipse Juno. I implemented in this way the Server-side implementation (TaskServiceImpl):

public class TaskServiceImpl extends ServiceImpl implements TaskService {

@Override
public List<Task> getAllTasks() {
    em = this.getEntityManager();
    Query q = em.createQuery("SELECT x FROM Task x");
    List<Task> list = createList(q.getResultList().toArray(),
            new ArrayList<Task>(), em);
    em.close();
    return list;
}

and this is the Database connection class in the client-side:

public class DatabaseConnection {
public static final TaskServiceAsync taskService;

static {
    taskService = GWT.create(TaskService.class);
}

}

I try now to run a getAllTask() in this way

public void onModuleLoad() {
    DatabaseConnection.taskService.getAllTasks(new AsyncCallback<List<Task>>() {

        @Override
        public void onSuccess(List<Task> result) {
            System.out.println("Success!");
        }

        @Override
        public void onFailure(Throwable caught) {
            System.out.println("Fail!");
        }
    });
}

And always returns "fail!" and gives me this error:

com.google.appengine.tools.development.LocalResourceFileServlet doGet WARNING: No file found for: /fantapgl/task

This is my web.xml

<servlet>
    <servlet-name>taskServiceImpl</servlet-name>
    <servlet-class>fieldProject.server.service.TaskServiceImpl</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>taskServiceImpl</servlet-name>
    <url-pattern>/fantaPGL/task</url-pattern>
  </servlet-mapping>

to open the connection to the DB I have this code in the persistence.xml:

<properties> 
  <property name="openjpa.jdbc.DBDictionary" value="postgres" /> 
  <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema()"/> 
  <property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/> 
  <property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/db" />
  <property name="openjpa.ConnectionUserName" value="postgres" /> 
  <property name="openjpa.ConnectionPassword" value="password" />
</properties>

I don't understand where I'm wrong. Can someone plz help me!?

ciaobetty
  • 93
  • 1
  • 1
  • 6

3 Answers3

0

Before executing the query:

1) Use jdbc driver

try {
       Class.forName("org.postgresql.Driver");
     } catch (ClassNotFoundException e) {

        System.out.println("Your PostgreSQL JDBC Driver is missing! "
                + "Include in your library path!");
     e.printStackTrace();
    return "error";

     }

2) Connect to database

Connection connection = null;
connection = 
    DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/"YourDB", 
                                 "admin", 
                                 "pass" );

3) Then Execute the query

if (connection != null) { //Your query }

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
istovatis
  • 1,408
  • 14
  • 27
  • I forgot to post how I establish the connection... I tried what you suggest but Connection is type "com.google.cloud.sql.jdbc.Connection"? because now I have a "com.google.cloud" cannot be resolved error. That's an extra library to add? Or is the cloud of G app engine? Because using PostgreSQL I don't thing that google.cloud will work – ciaobetty Dec 04 '12 at 08:46
0

I'm not sure what the problem is. But the error messages seems to suggest you have google appengine enabled. That doesn't make sense because you would only need that if you want to deploy on Google app engine, and you are clearly developing for something else since you can't run PostgreSql on Google appengine.

Futhremore, make sure to close database connections by placing the close in a finally statement and prefer to return specific datatypes; that is, ArrayList instead of List. Otherwise the compiler will generate code for all subclasses of List, because at compile time the compiler can't know what subclass will be used.

Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • So u mean that I have to deactivate the G app engine? it's activated by default and I didn't give it importance. The change from List to ArrayList that you suggest is necessary or only a better way to implement the code? – ciaobetty Dec 04 '12 at 08:42
  • Yes you need to deactive app engine if you don't develop for appengine if it's enabled, because it does extra checks related to the restrictions for G app engine. Using ArrayList is not necessary, but very much recommended due to the overhead of all subclasses of List that will be added to the generated code (meaning larger download for the end user of your script). – Hilbrand Bouwkamp Dec 04 '12 at 17:15
  • So I deactivated GAE e recreated the project, now I have this problem: `Starting Jetty on port 8888 [WARN] EXCEPTION java.lang.ClassNotFoundException: com.google.api.server.spi.SystemServiceServlet` and obviously many other lines starting with `at java.lang.ClassLoader.findClass(ClassLoader.java:358)` – ciaobetty Dec 05 '12 at 11:08
  • The Google plugin did generate a number of things when GAE is enabled. Among others configuration items in your web.xml (see http://stackoverflow.com/questions/12796589/how-to-disable-systemserviceservlet-configuration-in-app-engine-project) You need to just remove those dependencies as they are not relevant. – Hilbrand Bouwkamp Dec 06 '12 at 10:15
  • Thanks a lot Hilbrand you right, GAE was the main problem. OpenJPA doesn't work with GAE, basically if GAE is activated we have to use datanucleos – ciaobetty Dec 14 '12 at 15:02
0

you need to add @RemoteServiceRelativePath annotation at the begin of the serviceImpl class.

please refer to https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC or if you have installed google eclipse plugin, create a new project with sample code, you can refer to the sample code as well.

Weibo
  • 1,042
  • 8
  • 18