referring Tomcat JNDI datasource in persistence.xml

后端 未结 3 1266
梦如初夏
梦如初夏 2020-12-31 08:55

in server.xml I\'ve defined global resource (I\'m using Tomcat 6):


   

        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 09:24

    (Im using Apache OpenJPA library in Tomcat7 so may not match to Hibernate stuff)

    I have never used a global jdbc for my OpenJPA webapps but gave it a try. It worked and this is my configuration. See folder where persistence.xml is saved, probably is openjpa problem but without it nothing works.

    myapp/WEB-INF/classes/META-INF/persistence.xml
    this is using openjpa provider so class list may not be needed in hibernate.

     
    
    
    
        org.apache.openjpa.persistence.PersistenceProviderImpl
        java:comp/env/jdbc/mydb
    
        com.myapp.db.User
        com.myapp.db.Server
    
        
            
             
            
            
        
    
    
    
    

    tomcat/conf/server.xml
    Add global jdbc resource.

      ..cut...
      
        
    
        
      
      ..cut...
    

    tomcat/conf/Catalina/localhost/myapp.xml
    this my development box so I use docBase to link directly to projects folder. You should be find inserting this to war package (META-INF/context.xml) when deployed to production box.

    
    
    
      
    
      
    
    

    myapp/WEB-INF/web.xml
    Add resource-ref to the end of file.

    
    
    
      Webapp
      Webapp
      ..cut...
    
      
        mydb
        jdbc/mydb
        javax.sql.DataSource
        Container
      
    
    
    

    Because Im using OpenJPA+Tomcat7 (not fullsized j2ee container) this may look overengineering but thats how it works. Results are good and developing db-aware webapps is very easy. No need to manual sql query hardcoding and oldskool DAO classes.

提交回复
热议问题