i explain problem. try create web app using netbeans , tomcat. i've created backend of app provide services frontend. end connected postgresql db , manages entities persistence unit using eclipselink (jpa 2.1). use last version of each component.
now i'd use these services in frontend included .jar of backend in project. probleme in connecion between tomcat , db. after many hours of research i've come these config files :
web.xml :
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>controleurmaintenance</servlet-name> <servlet-class>controleur.actionservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>controleurmaintenance</servlet-name> <url-pattern>/controleurmaintenance</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>connection.html</welcome-file> </welcome-file-list> <resource-ref> <res-ref-name>java:jdbc/maintenancedb</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> <res-sharing-scope>shareable</res-sharing-scope> </resource-ref> </web-app> context.xml :
<?xml version="1.0" encoding="utf-8"?> <context path="jdbc/maintenance_webinterface"> <resource auth="container" name="java:comp/env/jdbc/maintenancedb" type="javax.sql.datasource" user="paul" password="1234" driverclassname="org.postgresql.driver" url="jdbc:postgresql://localhost:5432/maintenance_db" maxactive="20" schema="public" maxidle="2" maxawaits="-1" validationquery="select true;"/> </context> persistence.xml :
<?xml version="1.0" encoding="utf-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="maintenancepu" transaction-type="jta"> <provider>org.eclipse.persistence.jpa.persistenceprovider</provider> <jta-data-source>java:comp/env/jdbc/maintenancedb</jta-data-source> <properties> <property name="javax.persistence.schema-generation.database.action" value="create"/> </properties> </persistence-unit> </persistence> in brief i've tried consider data source. i've not touched server.xml. , got javax.naming.namenotfoundexception: name jdbc/maintenancedb not linked context. exception varied depending on change make. seems find db fails querying it...
any appreciated. thank much, if need more information, please ask.
try following in context.xml file please
<resource auth="container" driverclassname="org.postgresql.driver" maxactive="20" maxidle="2" maxwait="-1" name="/maintenancedb" password="1234" removeabandoned="true" removeabandonedtimeout="20" type="javax.sql.datasource" url="jdbc:postgresql://localhost:5432/maintenance_db" username="paul" validationquery="select true;"/> cheers anant
along here way bind jndi data source
try { // create initial context system.setproperty(context.initial_context_factory, "org.apache.naming.java.javaurlcontextfactory"); system.setproperty(context.url_pkg_prefixes, "org.apache.naming"); initialcontext ic = new initialcontext(); ic.createsubcontext("java:"); ic.createsubcontext("java:comp"); ic.createsubcontext("java:comp/env"); // construct datasource staging // construct datasource wfms pgsimpledatasource ds = new pgsimpledatasource(); ds.seturl("jdbc:postgresql://localhost:5432/maintenancedb");//you might context ds.setuser("paul");//you might context ds.setpassword("1234");//you might context ic.bind("java:comp/env/maintenancedb", wfmsds); } catch (namingexception ex) { ex.printstacktrace(); } might wanna add function in servlet init();
thanks anant
Comments
Post a Comment