jfinal多数据源的获取

andying 发布于 2013/12/19 14:53
阅读 4K+
收藏 0

我在configPlugin多配置了一个SQL2000的数据源,请问如何在BlogController中获取这个数据源?

(我想在应用中同时使用这两个数据源,依据需要获取相应数据源(mySQL,SQL-2000)中的表数据)

代码如下:

public void configPlugin(Plugins me) {
// 配置C3p0数据库连接池插件  mySQL
C3p0Plugin c3p0Plugin = new C3p0Plugin(
getProperty("jdbcUrl")
 , getProperty("user")
 , getProperty("password").trim());
me.add(c3p0Plugin);

// SQL2000
C3p0Plugin c3p0Plugin_sql2000= new C3p0Plugin(
getProperty("jdbcUrl_SQL2000")
,getProperty("user_SQL2000")
,getProperty("password_SQL2000").trim()
,getProperty("driver_SQL2000")
);
me.add(c3p0Plugin_sql2000);

加载中
0
JFinal
JFinal

引用来自“andying”的答案

我把mySql这个数据源去掉后,就可以正常连接SQL2000 了,也可以save了,怎样才能同时建立2个数据源连接?

jfinal 多数据源的问题请看这里:

http://www.oschina.net/question/5675_62719

http://www.oschina.net/question/925382_137846

jfinal 春节以后会考虑实现一个对用户完全透明的极简的多数据源方案

0
JFinal
JFinal

     在 YourJFinalConfig 中添加一个 public static C3p0Plugin c3p0Plugin,然后在 configPlugin(Plugins me)中赋上值: YourJFinalConfig.c3p0Plugin = c3p0Plugin;

    然后就可以在任意需要的地方这样引用: YourJFinalConfig.c3p0Plugin

0
andying
andying

报错:

2013-12-20 09:16:43
[WARN]-[Thread: com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0]-[com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded()]: Could not load driverClass com.microsoft.jdbc.sqlserver.SQLServerDrive
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDrive
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:100)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:132)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

sqljdbc4.jar包我已经加了,请问@JFinal 是什么原因?

上网查了说要设置什么CLASSPATH ,以前测试myBatis项目也没这么设置呀,也只是在XML配置一下drive赋以上值即可。

请问在jFinal中还需什么特别设置吗?


0
andying
andying

另:

我的配置文件内容如下:

jdbcUrl_SQL2000 = jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=test

user_SQL2000 =sa
password_SQL2000 =123
driver_SQL2000=com.microsoft.jdbc.sqlserver.SQLServerDrive

0
andying
andying

原因找到了是包名引用错误

应该是:

com.microsoft.sqlserver.jdbc.SQLServerDriver

0
andying
andying

问题又来了,

// 1.这句没问题:

List<Record> lists=Db.find(ds, "select * from Base_Addr");
for (Record record : lists) {
System.out.println(record.getInt("id")+" "+record.getStr("vAddr"));
}


-----------------------------------------------------------------------------------
Record  rec=new Record()
 .set("vAddr", "123").set("vCode", "aa");
Db.save(ds, "Base_Addr", rec);    // 2. 这句就有问题了,ds是连接SQL Server2000的数据源.报如下错误

com.microsoft.sqlserver.jdbc.SQLServerException: 第 1 行: '`' 附近有语法错误。


0
andying
andying

想显示一下SQL语句,做如下配置,又不成功.

//SQL2000
ActiveRecordPlugin arp2000=new ActiveRecordPlugin(c3p0Plugin_sql2000);
arp2000.setShowSql(true);
me.add(arp2000);

以上设置不行,

0
JFinal
JFinal

引用来自“andying”的答案

问题又来了,

// 1.这句没问题:

List<Record> lists=Db.find(ds, "select * from Base_Addr");
for (Record record : lists) {
System.out.println(record.getInt("id")+" "+record.getStr("vAddr"));
}


-----------------------------------------------------------------------------------
Record  rec=new Record()
 .set("vAddr", "123").set("vCode", "aa");
Db.save(ds, "Base_Addr", rec);    // 2. 这句就有问题了,ds是连接SQL Server2000的数据源.报如下错误

com.microsoft.sqlserver.jdbc.SQLServerException: 第 1 行: '`' 附近有语法错误。


设置方言: arp.setDialect(new AnsiSqlDialect());
0
andying
andying

我的配置类如下:

public void configPlugin(Plugins me) {

// 配置C3p0数据库连接池插件
C3p0Plugin c3p0Plugin = new C3p0Plugin(
getProperty("jdbcUrl")
 , getProperty("user")
 , getProperty("password").trim());
me.add(c3p0Plugin);

// 配置mySql
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
arp.setShowSql(true);
me.add(arp);
arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型


//配置SQL2000
C3p0Plugin c3p0Plugin_sql2000= new C3p0Plugin(
getProperty("jdbcUrl_SQL2000")
,getProperty("user_SQL2000")
,getProperty("password_SQL2000").trim()
,getProperty("driver_SQL2000")
);
me.add(c3p0Plugin_sql2000);
nConnSQL2000=c3p0Plugin_sql2000;

ActiveRecordPlugin arp2000=new ActiveRecordPlugin(c3p0Plugin_sql2000);
arp2000.setShowSql(true);
arp2000.setDialect(new AnsiSqlDialect());   // 配置方言出错呀,错误信息如下

me.add(arp2000);

}

出错:

Can not build TableInfo, maybe the table blog is not exists.


2013-12-20 14:09:51
[ERROR]-[Thread: main]-[com.jfinal.core.Config.startPlugins()]: Plugin start error: com.jfinal.plugin.activerecord.ActiveRecordPlugin. 
com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'blog' 无效。
com.jfinal.plugin.activerecord.ActiveRecordException: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'blog' 无效。
at com.jfinal.plugin.activerecord.TableInfoBuilder.buildTableInfo(TableInfoBuilder.java:49)
at com.jfinal.plugin.activerecord.ActiveRecordPlugin.start(ActiveRecordPlugin.java:118)
at com.jfinal.core.Config.startPlugins(Config.java:78)
at com.jfinal.core.Config.configJFinal(Config.java:48)
at com.jfinal.core.JFinal.init(JFinal.java:68)
at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3696)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'blog' 无效。



0
andying
andying

说明一下:

     1. Blog表指向的是mySql数据源

     2.Record  rec=new Record()
          .set("vAddr", "123").set("vCode", "aa");
      Db.save(ds, "Base_Addr", rec);  

      这里的ds指向的是MS-SQL2000 Server数据源


     

返回顶部
顶部