2013-12-16 67 views
5

我正在通過Gilberto T. Garcia Jr的Lift應用程序開發指南,並遇到了一個我似乎無法解決的問題。我已經複製了源代碼Chap06-map-table,我試圖修改它以與我的IBM i(iSeries,AS/400,i5)數據庫一起工作。我能夠使用Squeryl Record的第一種連接方式進行工作。但是,我似乎無法弄清楚如何使用JNDI數據源來工作。我花了幾天的時間在互聯網上搜索設置的例子,並沒有找到一個涉及DB/400數據庫連接的好例子。以下是我嘗試啓動容器時出現的錯誤,以及我爲了使其正常工作而修改的代碼。任何幫助,將不勝感激。 jt4oo.jar(jtOpen)中的數據源類似乎有一些選擇,我不確定哪個是最好用的,或者是另一個。我一直在嘗試與這三個中的每一個,並假設第一個是正確的。請求者無法建立連接。 Jetty,Lift/Scala,iSeries DB2/400

com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource 
com.ibm.as400.access.AS400JDBCConnectionPoolDataSource 
com.ibm.as400.access.AS400JDBCDataSource 

謝謝。鮑勃

這是錯誤的開始:

> container:start 
[info] jetty-8.0.4.v20111024 
[info] No Transaction manager found - if your webapp requires one, please config 
ure one. 
[info] NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet 
[info] started o.e.j.w.WebAppContext{/,[file:/C:/Users/Bob/Lift26Projects/scala_ 
210/chap06-map-table/src/main/webapp/]} 
[info] started o.e.j.w.WebAppContext{/,[file:/C:/Users/Bob/Lift26Projects/scala_ 
210/chap06-map-table/src/main/webapp/]} 
18:21:47.062 [pool-7-thread-1] ERROR n.liftweb.http.provider.HTTPProvider - Fail 
ed to Boot! Your application may not run properly 
java.sql.SQLException: The application requester cannot establish the connection 
. ("jdbc:as400://www.busapp.com;libraries=PLAY2TEST";naming=system;errors=full;) 
     at com.ibm.as400.access.JDError.throwSQLException(JDError.java:524) ~[jt 
400-6.7.jar:JTOpen 6.7] 
     at com.ibm.as400.access.AS400JDBCConnection.setProperties(AS400JDBCConne 
ction.java:3142) ~[jt400-6.7.jar:JTOpen 6.7] 
     at com.ibm.as400.access.AS400JDBCManagedDataSource.createPhysicalConnect... 

我Build.sbt文件:

name := "Lift 2.5 starter template" 
version := "0.0.1" 
organization := "net.liftweb" 
scalaVersion := "2.10.0" 
resolvers ++= Seq("snapshots" at "http://oss.sonatype.org/content/repositories/snapshots", 
      "staging"   at "http://oss.sonatype.org/content/repositories/staging", 
      "releases"  at "http://oss.sonatype.org/content/repositories/releases" 
      ) 
seq(com.github.siasia.WebPlugin.webSettings :_*) 
unmanagedResourceDirectories in Test <+= (baseDirectory) { _/"src/main/webapp" } 
scalacOptions ++= Seq("-deprecation", "-unchecked") 
env in Compile := Some(file("./src/main/webapp/WEB-INF/jetty-env.xml") asFile) 
libraryDependencies ++= { 
    val liftVersion = "2.5" 
    Seq(
    "net.liftweb"  %% "lift-webkit"  % liftVersion  % "compile", 
    "net.liftmodules" %% "lift-jquery-module_2.5" % "2.3", 
    "org.eclipse.jetty"  % "jetty-webapp"  % "8.0.4.v20111024" % "container", 
    "org.eclipse.jetty"  % "jetty-plus"   % "8.0.4.v20111024" % "container", 
    "ch.qos.logback" % "logback-classic"  % "1.0.6", 
    "org.specs2"  %% "specs2"    % "1.14"   % "test", 
    "net.liftweb"  %% "lift-squeryl-record" % liftVersion % "compile", 
    "net.sf.jt400" % "jt400"  % "6.7", 
    "org.liquibase" % "liquibase-maven-plugin" % "3.0.2" 
) 
} 

這是我boot.scala文件:

package bootstrap.liftweb 

    import _root_.liquibase.database.DatabaseFactory 
    import _root_.liquibase.database.jvm.JdbcConnection 
    import _root_.liquibase.exception.DatabaseException 
    import _root_.liquibase.Liquibase 
    import _root_.liquibase.resource.FileSystemResourceAccessor 
    import net.liftweb._ 
    import util._ 
    import Helpers._ 
    import common._ 
    import http._ 
    import sitemap._ 
    import Loc._ 
    import net.liftmodules.JQueryModule 
    import net.liftweb.http.js.jquery._ 
    import net.liftweb.squerylrecord.SquerylRecord 
    import org.squeryl.Session 
    import java.sql.{SQLException, DriverManager} 
    import org.squeryl.adapters.DB2Adapter 
    import javax.naming.InitialContext 
    import javax.sql.DataSource 
    import code.model.LiftBookSchema 
    /** 
    * A class that's instantiated early and run. It allows the application 
    * to modify lift's environment 
    */ 
    class Boot { 
    def runChangeLog(ds: DataSource) { 
    val connection = ds.getConnection 
     try { 
     val database = DatabaseFactory.getInstance(). 
     findCorrectDatabaseImplementation(new JdbcConnection(connection)) 
     val liquibase = new Liquibase(
     "database/changelog/db.changelog-master.xml", 
     new FileSystemResourceAccessor(), 
     database 
     ) 

     liquibase.update(null) 
     } catch { 
     case e: SQLException => { 
     connection.rollback() 
     throw new DatabaseException(e) 
     } 
    } 
    } 
    def boot { 

    // where to search snippet 
    LiftRules.addToPackages("code") 

    prepareDb() 


    // Build SiteMap 
    val entries = List(
     Menu.i("Home")/"index", // the simple way to declare a menu 

     // more complex because this menu allows anything in the 
     // /static path to be visible 
     Menu(Loc("Static", Link(List("static"), true, "/static/index"), 
     "Static Content"))) 

    // set the sitemap. Note if you don't want access control for 
    // each page, just comment this line out. 
    LiftRules.setSiteMap(SiteMap(entries: _*)) 

    //Show the spinny image when an Ajax call starts 
    LiftRules.ajaxStart = 
     Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd) 

    // Make the spinny image go away when it ends 
    LiftRules.ajaxEnd = 
     Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd) 

    // Force the request to be UTF-8 
    LiftRules.early.append(_.setCharacterEncoding("UTF-8")) 

    // Use HTML5 for rendering 
    LiftRules.htmlProperties.default.set((r: Req) => 
     new Html5Properties(r.userAgent)) 

    //Init the jQuery module, see http://liftweb.net/jquery for more information. 
    LiftRules.jsArtifacts = JQueryArtifacts 
    JQueryModule.InitParam.JQuery = JQueryModule.JQuery172 
    JQueryModule.init() 

    } 

    def prepareDb() { 

    Class.forName("com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource") 

    val ds = new InitialContext().lookup("java:/comp/env/jdbc/dsliftbook").asInstanceOf[DataSource] 

    runChangeLog(ds) 

    SquerylRecord.initWithSquerylSession(
     Session.create(
     ds.getConnection, 
     new DB2Adapter) 
    ) 
    } 


} 

這是我的碼頭,ENV-XML文件

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
    <New id="dsliftbook" class="org.eclipse.jetty.plus.jndi.Resource"> 
    <Arg></Arg> 
    <Arg>jdbc/dsliftbook</Arg> 
    <Arg> 
     <New class="com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource"> 
     <Set name="serverName">"jdbc:as400://www.[server].com;libraries=PLAY2TEST";naming=system;errors=full;</Set> 
     <Set name="user">[user]</Set> 
     <Set name="password">[password]</Set> 
     </New> 
    </Arg> 
    </New> 
</Configure> 
+2

將您從JDBC驅動程序一個SQLException告訴你,它不能連接到數據庫。檢查您的網址,用戶名和密碼是否正確。如果它們看起來不錯,嘗試從運行應用程序的機器連接到服務器,而不使用java並查看是否成功。您也可以找到更多關於db爲什麼拒絕db日誌中的連接的信息。 –

+0

引用似乎錯誤放置在您的數據庫uri上:'「jdbc:as400:// www。[server] .com; libraries = PLAY2TEST」; naming = system; errors = full;'應該是'「jdbc:as400:// www。[server] .com; libraries = PLAY2TEST; naming = system; errors = full;「' – jamesallman

+0

我已經修復了這個引用,事實上完全刪除了它們。仍然不起作用。對於非數據源連接,以下工作。 '進口org.squeryl.Session 進口org.squeryl.adapters.DB2Adapter 進口net.liftweb.squerylrecord.SquerylRecord 的Class.forName( 「com.ibm.as400.access.AS400JDBCDriver」) DEF連接= DriverManager的。 getConnection(「jdbc:as400:// www。{server] .com /; naming = 1; libraries = PLAY2TEST;」,「[user]」,「[password]」) SquerylRecord.initWithSquerylSession(Session.create連接,新的DB2Adapter))' –

回答

1

好吧,我已經成功地得到連接。一個問題是jetty-env-xml文件中的引號。而且我使用的用戶名/密碼顯然沒有使這項工作成爲必需的權限我不知道爲什麼,因爲這是我用於所有iSeries開發的相同ID /密碼。所以現在,我是具有安全管理員權限的另一個用戶配置文件,直到我能夠弄清楚發生了什麼或者需要什麼權限。

一旦我登錄,我無法爲用戶設置庫列表,導致SQL失敗。它正在查找與用戶標識相同的庫名稱。目前,我已經通過創建一個與用戶標識相同的新庫來解決此問題。

這裏的另一個問題是,即使我提供了ID和密碼,在系統連接之前,系統會提示我輸入ID /密碼。 ID和URL被填入,但密碼總是需要被重新鍵入。

我已經包含jetty-env-xml文件和boot.scala文件的當前源代碼。希望這可以幫助其他人。

感謝戴夫和詹姆斯的幫助!

鮑勃

啓動。斯卡拉:

package bootstrap.liftweb 

// import _root_.liquibase.database.DatabaseFactory 
// import _root_.liquibase.database.jvm.JdbcConnection 
// import _root_.liquibase.exception.DatabaseException 
// import _root_.liquibase.Liquibase 
// import _root_.liquibase.resource.FileSystemResourceAccessor 
import net.liftweb._ 
import util._ 
import Helpers._ 

import common._ 
import http._ 
import sitemap._ 
import Loc._ 
import net.liftmodules.JQueryModule 
import net.liftweb.http.js.jquery._ 
import net.liftweb.squerylrecord.SquerylRecord 
import org.squeryl.Session 
import java.sql.{SQLException, DriverManager} 
import org.squeryl.adapters.DB2Adapter 
import javax.naming.InitialContext 
import javax.sql.DataSource 
import code.model.LiftBookSchema 
import com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource 



/** 
* A class that's instantiated early and run. It allows the application 
* to modify lift's environment 
*/ 
class Boot { 
    // def runChangeLog(ds: DataSource) { 
    // val connection = ds.getConnection 

    // try { 
    //  val database = DatabaseFactory.getInstance(). 
    //  findCorrectDatabaseImplementation(new JdbcConnection(connection)) 

    //  val liquibase = new Liquibase(
    //  "database/changelog/db.changelog-master.xml", 
    //  new FileSystemResourceAccessor(), 
    //  database 
    // ) 

    //  liquibase.update(null) 
    // } catch { 
    //  case e: SQLException => { 
    //  connection.rollback() 
    //  throw new DatabaseException(e) 
    //  } 
    // } 
    // } 

    def boot { 

    // where to search snippet 
    LiftRules.addToPackages("code") 

    prepareDb() 


    // Build SiteMap 
    val entries = List(
     Menu.i("Home")/"index", // the simple way to declare a menu 

     // more complex because this menu allows anything in the 
     // /static path to be visible 
     Menu(Loc("Static", Link(List("static"), true, "/static/index"), 
     "Static Content"))) 

    // set the sitemap. Note if you don't want access control for 
    // each page, just comment this line out. 
    LiftRules.setSiteMap(SiteMap(entries: _*)) 

    //Show the spinny image when an Ajax call starts 
    LiftRules.ajaxStart = 
     Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd) 

    // Make the spinny image go away when it ends 
    LiftRules.ajaxEnd = 
     Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd) 

    // Force the request to be UTF-8 
    LiftRules.early.append(_.setCharacterEncoding("UTF-8")) 

    // Use HTML5 for rendering 
    LiftRules.htmlProperties.default.set((r: Req) => 
     new Html5Properties(r.userAgent)) 

    //Init the jQuery module, see http://liftweb.net/jquery for more information. 
    LiftRules.jsArtifacts = JQueryArtifacts 
    JQueryModule.InitParam.JQuery = JQueryModule.JQuery172 
    JQueryModule.init() 

    } 

    def prepareDb() { 

    Class.forName("com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource") 

    val ds = new InitialContext().lookup("java:/comp/env/jdbc/dsliftbook").asInstanceOf[DataSource] 

    // runChangeLog(ds) 

    SquerylRecord.initWithSquerylSession(Session.create(ds.getConnection, new DB2Adapter) 
    ) 
    } 
} 

碼頭-ENV-XML

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
    <New id="dsliftbook" class="org.eclipse.jetty.plus.jndi.Resource"> 
    <Arg></Arg> 
    <Arg>jdbc/dsliftbook</Arg> 
    <Arg> 
     <New class="com.ibm.as400.access.AS400JDBCManagedConnectionPoolDataSource"> 
     <Set name="serverName">www.[server].com</Set> 
     <Set name="user">DBUSER</Set> 
     <Set name="password">DBUSER</Set> 
     </New> 
    </Arg> 
    </New> 
</Configure> 
+0

我也能夠作爲QUSER登錄。我的個人資料似乎有些不同,這使我無法使用它進行登錄。 –