Montag, 5. Mai 2014

MongoDB, EclipseLink and Eclipse - a Database connection for our Webapp

Hey everyone,

this is the second part of my tutorial on how to set up a development environment for 3 tier web application development. This post is just a collection of step by step instructions until now. In the next days I will add some more descriptive text, but right now I'm pretty busy at work ;)

Nevertheless, I hope you can draw something useful from these step by step instructions until then.

Assumptions: 
Installing and configuring :
  • MongoDB 2.6.x
  • MonjaDB
  • EclipseLink 2.5
Instructions:

How to setup MongoDB

  • Go to http://www.mongodb.org/downloads -> Download Windows 64bit (32bit)msi installer 
  • Run the installer -> Next -> check License Agreement -> Next -> Custom -> Choose your favorite install location -> Next -> Install -> Finish
  • Go to your installation directory and create the folder structure "/data/db".
  • Go to your installation directory and create a folder named "log".
  • In the installation directory create a file named "mongod.cfg" -> open file -> type the mongod.cfg snippet (see below)
  • In the installation directory create a file named "mongoStart.bat" -> open file (e.g. with notepad) -> type "C:\path\to\mongo\bin\mongod.exe --config C:\path\to\mongo\mongod.cfg"
  • DB Startup: doubleclick mongoStart.bat
  • DB Shutdown: in command prompt type CTRL+C -> J -> Enter
  • All log files are created in the log directory and can be read with notepad or a text editor
  • (optional) If you are running Windows7 AND you are getting Errorcode 33 while running MongoDB -> Installhttp://support.microsoft.com/kb/2731284

How to install MonjaDB Eclipse Plugin
  • Install MonjaDB Plugin
    • Help -> Eclipse Marketplace -> type "MonjaDB" -> Install -> Confirm -> I accept the terms of the license agreement -> Finish (Installs Monja DB for easier DB development)
    • Window -> Perspectives -> Other -> MonjaDB (opens the perspective for Monja DB. One can switch back to the normal perspective by clicking on "JavaEE" in the right top corner of Eclipse)
    • To connect to a running MongoDB click on the leftmost icon of the "DB Tree" view -> ConnectionType=Normal / No SSH Configuration / Host=127.0.0.1 / Port=27017 / Database=test -> OK

How to configure a DynamicWebProject for MongoDB + EclipseLink
  • Setup MongoDB and EclipseLink 
    • R-Click project -> Maven -> Add Dependency -> org.eclipse.persistence.jpa / org.eclipse.persistence.nosql / mongo-java-driver / org.apache.tomcat servlet api / javax javaee-api
    • R-Click on "src" folder -> New -> Other -> General -> Folder -> Name it "META-INF"
    • R-Click on "META-INF" folder -> New -> Other -> File -> Name it "persistence.xml" (This is where you are configuring )
    • insert persistence.xml snippet (see below)

Snippets:
mongod.cfg

systemLog:
 destination: file
 path: 'C:\\path\\to\\mongo\\log\\mongo.log'
storage:
 dbPath: 'C:\\path\\to\\mongo\\data'



persistence.xml
<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 persistence_2_0.xsd" version="2.0"> 
<persistence-unit name="<unitname>" transaction-type="RESOURCE_LOCAL"> 
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<exclude-unlisted-classes>false</exclude-unlisted-classes> 
<properties> 
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/> 
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/> 
<property name="eclipselink.nosql.property.mongo.port" value="<xxxxx>, 27017"/>
<property name="eclipselink.nosql.property.mongo.host" value="<host1>, localhost"/> 
<property name="eclipselink.nosql.property.mongo.db" value="<dbname>"/> 
<property name="eclipselink.logging.level" value="FINEST"/>
</properties> 
</persistence-unit> 
</persistence> 

References

Keine Kommentare:

Kommentar veröffentlichen