Berawal pada saat ingin running aplikasi web spring menggunakan mvn jetty:run.
Awalnya baik baik saja. kemudian muncul masalah ketika source code di repository jdbc connection ke MySQL diganti menggunakan JNDI yg dipasang di jetty server.
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/SampleDB"
expected-type="javax.sql.DataSource" />
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="testPersistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Ketika di run lewat mvn jetty terjadi error, karena JNDI name tidak temukan.
2015-06-19 11:07:04.474:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@7270fd39{/organika,file:/Data/Data-Project/Nostra/Organika/sources/nostratech-farming-server/nostra-web/src/main/webapp/,STARTING}{file:/Data/Data-Project/Nostra/Organika/sources/nostratech-farming-server/nostra-web/src/main/webapp/}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [/Data/Data-Project/Nostra/Organika/sources/nostratech-farming-server/nostra-web/target/classes/spring/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException; remaining name 'jdbc/SampleDB'
.....
Caused by:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException; remaining name 'jdbc/SampleDB'
Salah satu solusinya adalah saya harus menginstall standalone jetty dan menambahkan JNDI config di jetty nya. Karena hal itu cukup rumit maka saya tidak melakukannya.
Cara lain yg saya lakukan adalah memasang JNDI di maven jetty plugin.
Di folder resource kita buat file jetty-env.xml.
Di dalamnya kita definisikan JNDI yg kita pakai name nya kita samakan saja dengan yg dipakai di server.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id ="sampledb" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="SampleDB" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/sampleDB</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://localhost:3306/test_db</Set>
<Set name="User">root</Set>
<Set name="Password"></Set>
</New>
</Arg>
</New>
</Configure>
Setelah itu kita panggil jetty-env.xml itu di config jetty plugin (pom.xml)
di configuration :
<jettyXml>src/main/resources/jetty-env.xml</jettyXml>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.2.v20140210</version>
<configuration>
<jettyXml>src/main/resources/jetty-env.xml</jettyXml>
<webAppConfig>
<contextPath>/${war.name}</contextPath>
</webAppConfig>
<stopKey/>
<stopPort/>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
Kita running lagi mvn jetty:run.
Yes akhirnya bisa juga.
Referensi : https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
https://wiki.eclipse.org/Jetty/Feature/JNDI
Selamat mencoba.
No comments:
Post a Comment