Paulo
Paulo

Reputation: 1498

POST not inserting data in database

I'm stuck on a webservice project. I have a resource class with 5 methods (4 GET methods and 1 POST method). Every single GET method works (that is, pulling data from the database is fine). But my POST method doesn't work (that's where the data is pushing to the database). The problem is occurring on Hibernate commit.

I'm using jdk 7, tomcat 6.0.41, jersey 2.x.

My POST method's code is:

@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response postRequisicao(@FormParam("uuid") String uuid, @FormParam("geometria") String geometria, @FormParam("itens") String itensId) {
    try {
        LocalChecklist localChecklist = new LocalChecklist(uuid, new Date(), (Point) wktToGeometry(geometria));
        LocalItemChecklistDAO.save(localChecklist, itensId);
    } catch(Exception e) {
        return Response.serverError().build();
    }
    return Response.ok().build();
}

My LocalChecklistDAO.save() method's code is:

public static void save(LocalChecklist localChecklist, String itensId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(localChecklist);
    for(ItemChecklist itemChecklist: ItemChecklist.getListFromIds(itensId)) {
        LocalItemChecklist item = new LocalItemChecklist(localChecklist, itemChecklist);
        session.save(item);
    }
    session.getTransaction().commit();
    session.close();
}

The exception sent in the line session.getTransaction().commit(); is:

SEVERE: Servlet.service() for servlet Jersey REST Service threw exception
org.hibernate.spatial.helper.FinderException: Tried retrieving OracleConnection from oracle.jdbc.driver.T4CConnection using method _getPC, but received null.
    at org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:75)
    at org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:44)
    at org.hibernate.spatial.dialect.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:119)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.store(SDOGeometryValueBinder.java:76)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.toNative(SDOGeometryValueBinder.java:83)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.bind(SDOGeometryValueBinder.java:66)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.bind(SDOGeometryValueBinder.java:52)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at dao.LocalItemChecklistDAO.save(LocalItemChecklistDAO.java:23)
    at resources.ResourceTester.adicionarLocal2(ResourceTester.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:745)

One thing I think it's worth to mention is: before committing, I can see the objects CAN get its ID from the database sequence, that is, before save(), its ID is null (as expected), and after save() (and before commit()) the object gets the exact ID it should get (from the sequence, as expected).

Can someone see something?

Upvotes: 0

Views: 161

Answers (2)

Paulo
Paulo

Reputation: 1498

AT LAST!

I was searching for the problem since yesterday morning (my local time, UTC-3). I think that's a very specific case, in which, I removed ojdbc14.jar from tomcat's lib, and added ojdbc6.jar and it worked.

I don't know if this is a bug, but it's the second time that I get into problem using ojdbc14.jar. I don't remember what was the first problem I had using ojdbc14.jar, but I do remember that I had a problem with it.

I'll leave my answer here in case of someone in the future faces this problem.

Upvotes: 1

sgpalit
sgpalit

Reputation: 2686

I think you have to seperate your transcation, and commit on the tx you receive.

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
Session session = sessionFactory.openSession(); 
Transaction tx = session.beginTransaction();
session.save(item);
tx.commit();

Upvotes: 1

Related Questions