Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    JPA - Using datsource instead of direct JDBC

    I am using SmartGWT 2.4 (2011.01.05). Trying to get the JPADataSource to work. Application is running on Geronimo 2.2, which manages the database connection pool to a MS SQL 2008 DB.

    The server.properties looks like this:

    Code:
    webRoot: __AUTODETECT__
    
    sql.defaultDatabase:sqlserver
    sql.SQLServer.driver:net.sourceforge.jtds.jdbc.Driver
    
    project.datasources: $webRoot/ds, $webRoot/shared/ds
    project.ui: $webRoot/shared/ui
    project.apps: $webRoot/shared/app
    
    jpa.emfProvider:com.isomorphic.jpa.EMFProviderLMT
    jpa.persistenceUnitName:TestSqlServices
    
    RPCManager.enabledBuiltinMethods: *
    The persistence.xml looks like this:

    Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                 version="2.0">
      <persistence-unit name="TestServices" transaction-type="JTA">
        <jta-data-source>jdts/testDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="openjpa.ConnectionDriverName" value="net.sourceforge.jtds.jdbc.Driver"/>
          <property name="openjpa.ConnectionURL" value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=testDB"/>
          <property name="openjpa.ConnectionUserName" value="sa"/>
          <property name="openjpa.ConnectionPassword" value="password"/>    
        </properties>
      </persistence-unit>
    </persistence>
    The problem is, it is not using the <jta-data-source> value to access the DB, instead it is trying to use the values specified in the properties to access the DB using direct JDBC.

    #2
    The problem is most likely that your .ds.xml file does not specify that it is a JPA DataSource. However, bear in mind, SQLDataSource is the recommend approach rather than JPA (for the many reasons listed in the QuickStart Guide). Also, a 2.5 release is available which you should upgrade to.

    Comment


      #3
      &quot;No active transaction&quot; problem

      Thanks for the prompt reply. Our product is closed to its release date, so we don't think it is a good time to upgrade to 2.5.

      After trying a few more things, i think i have made some progress:
      1. Change to jpa.emfProvider:com.isomorphic.jpa.EMFProviderCMT instead of LMT
      2. Added the following to server.properties:
      Code:
      jpa.cmt.entityManager: persistence/em
      jpa.cmt.transaction: persistence/tx
      3. Added the following to web.xml:
      Code:
        <persistence-context-ref>
          <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
          <persistence-unit-name>TestServices</persistence-unit-name>
        </persistence-context-ref>
      
        <resource-env-ref>
          <resource-env-ref-name>persistence/tx</resource-env-ref-name>
          <resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
        </resource-env-ref>
      It is not using the direct JDBC properties anymore, and JPA seem to be working. However, I am getting the following error:
      Code:
      === 2011-10-10 09:27:27,478 [l 98] WARN  RequestContext - dsRequest.execute() failed: 
      javax.persistence.TransactionRequiredException: No active transaction
      	at org.apache.geronimo.persistence.CMPEntityManagerTxScoped.getEntityManager(CMPEntityManagerTxScoped.java:48)
      	at org.apache.geronimo.persistence.CMPEntityManagerTxScoped.persist(CMPEntityManagerTxScoped.java:81)
      	at com.isomorphic.jpa.JPADataSource.executeAdd(JPADataSource.java:452)
      	at com.test.UploadDataSource.executeAdd(UploadDataSource.java:120)
      	at com.isomorphic.datasource.DataSource.execute(DataSource.java:1050)
      	at com.isomorphic.jpa.JPADataSource.execute(JPADataSource.java:218)
      	at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:721)
      	at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:658)
      	at com.isomorphic.application.AppBase.execute(AppBase.java:491)
      	at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1443)
      	at com.isomorphic.servlet.IDACall.handleDSRequest(IDACall.java:173)
      	at com.isomorphic.servlet.IDACall.processRPCTransaction(IDACall.java:130)
      	at com.isomorphic.servlet.IDACall.processRequest(IDACall.java:95)
      	at com.isomorphic.servlet.IDACall.doPost(IDACall.java:54)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
      	at com.isomorphic.servlet.BaseServlet.service(BaseServlet.java:152)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at com.test.filter.TestFilter.doFilter(TestFilter.java:35)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	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.geronimo.tomcat.security.SecurityValve.invoke(SecurityValve.java:88)
      	at org.apache.geronimo.tomcat.security.jacc.JACCSecurityValve.invoke(JACCSecurityValve.java:54)
      	at org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:420)
      	at org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
      	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:849)
      	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:361)
      	at org.apache.geronimo.pool.ThreadPool$1.run(ThreadPool.java:214)
      	at org.apache.geronimo.pool.ThreadPool$ContextClassLoaderRunnable.run(ThreadPool.java:344)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      	at java.lang.Thread.run(Thread.java:662)
      === 2011-10-10 09:27:27,479 [l 98] WARN  EMFProviderCMT - Unexpected exception while rolling back transaction
      java.lang.IllegalStateException: No transaction associated with current thread
      	at org.apache.geronimo.transaction.manager.TransactionManagerImpl.setRollbackOnly(TransactionManagerImpl.java:239)
      	at com.isomorphic.jpa.EMFProviderCMT.rollbackTransaction(EMFProviderCMT.java:203)
      	at com.isomorphic.jpa.EMF.rollbackTransaction(EMF.java:142)
      	at com.isomorphic.jpa.JPADataSource.onFailure(JPADataSource.java:655)
      	at com.isomorphic.rpc.RPCManager.onFailure(RPCManager.java:1303)
      	at com.isomorphic.rpc.RPCManager.onFailure(RPCManager.java:1203)
      	at com.isomorphic.rpc.RPCManager.completeResponse(RPCManager.java:962)
      	at com.isomorphic.rpc.RPCManager.send(RPCManager.java:582)
      	at com.isomorphic.servlet.IDACall.processRPCTransaction(IDACall.java:130)
      	at com.isomorphic.servlet.IDACall.processRequest(IDACall.java:95)
      	at com.isomorphic.servlet.IDACall.doPost(IDACall.java:54)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
      	at com.isomorphic.servlet.BaseServlet.service(BaseServlet.java:152)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at com.test.filter.TestFilter.doFilter(TestFilter.java:35)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	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.geronimo.tomcat.security.SecurityValve.invoke(SecurityValve.java:88)
      	at org.apache.geronimo.tomcat.security.jacc.JACCSecurityValve.invoke(JACCSecurityValve.java:54)
      	at org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:420)
      	at org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
      	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:849)
      	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:361)
      	at org.apache.geronimo.pool.ThreadPool$1.run(ThreadPool.java:214)
      	at org.apache.geronimo.pool.ThreadPool$ContextClassLoaderRunnable.run(ThreadPool.java:344)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      	at java.lang.Thread.run(Thread.java:662)
      Any ideas on how to get around this error?
      Last edited by mathew.yap; 9 Oct 2011, 19:31.

      Comment


        #4
        If you're setting up new access to JPA, upgrade to 2.5 for various improvements, including better error reporting.

        If you still need help after this, you need to post the full server log for the request (always do this), along with all relevant JPA config.

        Comment


          #5
          Hi,

          Have to admit that docs here are misleading (and implementation is not correct).

          Just committed changes (should appear in next nightly build).

          You will have to use EMFProviderBMT (new).

          To use Bean Managed Transactions you have to configure:

          server.properties:
          Code:
          jpa.emfProvider: com.isomorphic.jpa.EMFProviderBMT
          jpa.entityManager: persistence/em
          jpa.entityManagerFactory: persistence/emf
          web.xml
          Code:
            <persistence-context-ref>
                <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
                <persistence-unit-name>PERSISTENCE_UNIT_NAME</persistence-unit-name>
            </persistence-context-ref>
            <persistence-unit-ref>
                <persistence-unit-ref-name>persistence/emf</persistence-unit-ref-name>
                <persistence-unit-name>PERSISTENCE_UNIT_NAME</persistence-unit-name>
            </persistence-unit-ref>

          Now EMFProviderCMT should be used only when you are accessing data source from EJB.

          Best reagrds,
          Alius

          Comment

          Working...
          X