创建自定义dataSourceClassName

时间:2016-02-26 23:08:11

标签: java spring hibernate playframework hikaricp

我有一个使用playFramework的应用程序,Spring&像这样的Hibernate:

Play-Spring-Data-JPA-APP

我想用AbstractRoutingDataSource实现多个数据源,所以我有自定义类:

package mx;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 *
 */
public class MyRouting extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        System.out.println("Logic to determine the datasource");
        return "key";
    }
}

但是当我尝试在我的application.conf中设置数据源时,如下所示: db.routingDS.dataSourceClassName = mx.MyRouting

我得到了

[error] play - Error while stopping the application
play.api.PlayException: Cannot load plugin[An exception occurred during Plugin [com.edulify.play.hikaricp.HikariCPPlugin] initialization]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:154) ~[play_2.10-2.3.10.jar:2.3.10]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10-2.3.10.jar:2.3.10]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:0.13.5]
Caused by: play.api.Configuration$$anon$1: Configuration error[java.lang.ClassNotFoundException: mx.MyRouting]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94) ~[play_2.10-2.3.10.jar:2.3.10]
at play.api.Configuration.reportError(Configuration.scala:743) ~[play_2.10-2.3.10.jar:2.3.10]
at com.edulify.play.hikaricp.HikariCPDBApi$$anonfun$2.apply(HikariCPDBApi.scala:64) ~[play-hikaricp_2.10-2.0.6.jar:2.0.6]
at com.edulify.play.hikaricp.HikariCPDBApi$$anonfun$2.apply(HikariCPDBApi.scala:44) ~[play-hikaricp_2.10-2.0.6.jar:2.0.6]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: mx.MyRouting
at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:120) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.PoolUtilities.initializeDataSource(PoolUtilities.java:102) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:156) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48) ~[HikariCP-2.3.8.jar:na]
Caused by: java.lang.ClassNotFoundException: mx.MyRouting
at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_25]
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[na:1.8.0_25]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_25]
at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[na:1.8.0_25]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_25]

(我也使用HikariCP的db配置)

我也尝试用类生成一个jar,结果是一样的,任何人都有一些想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

通过直接播放应用程序配置无法实现您想要实现的目标。主要问题是默认播放期望URL和驱动程序创建数据源(至少在播放2.3和播放2.4中)。

换句话说,play总是希望你要创建一个数据源,你将使用连接池到真正的数据库而不是RoutingDatasource。

由于您使用的是JPA种子,因此需要通过JNDI访问资源,因此需要以某种方式将数据源添加到JNDI。

这是如何实现的:

在application.conf中,只声明将与数据库交互的数据源。尽可能多的人:

# Database configuration
#
db.mydb1.driver=org.h2.Driver
db.mydb1.url="jdbc:h2:mem:play"
db.mydb1.jndiName=DB1DS
db.mydb2.driver=org.h2.Driver
db.mydb2.url="jdbc:h2:mem:play"
db.mydb2.jndiName=DB2DS

现在,在persistence.xml中确保您不声明任何数据源。 (既不是jta也不是jta)

<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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

最后,在Global.java或Global.scala中,强制进入JNDI的RoutingDatasource。令人讨厌的部分是,没有文档或发布任何有关Play如何管理JNDI的信息。基本上,Play使用类&#34; InitialContext&#34;创建一个内存JNDI。这个上下文是可写的(感谢上帝),因为有许多应用程序服务器可以保护它们的初始上下文。

//This is the routing Datasource in spring context
@Bean
public RoutingDatasource routingDataSource() {
  RoutingDatasource rd = new RoutingDatasource();
  java.util.HashMap ds = new java.util.HashMap();
  ds.put("DB1DS","DB1DS");
  ds.put("DB2DS","DB2DS");
  rd.setTargetDataSources(ds);
  return rd;
}

@Bean
public EntityManagerFactory entityManagerFactory() {
  java.util.Map props = new java.util.HashMap();  
  try{
//This is the magic line.
  ((javax.naming.Context)(new javax.naming.InitialContext())).bind("routingDataSource", ctx.getBean("routingDataSource"));
   }catch(Exception e){}
  props.put("javax.persistence.jtaDataSource", "routingDataSource");
            return Persistence.createEntityManagerFactory(DEFAULT_PERSISTENCE_UNIT,props);
}

最后就像你提到的那样,创建你自己的RoutingDatasource:

public class RoutingDatasource extends AbstractRoutingDataSource {

   @Override
   protected Object determineCurrentLookupKey() {
     return selectRandom("DB1DS","DBDS2");
   }
}

多数民众赞成。我希望它有助于解决您的问题。