2014-06-16 98 views
0

在JPAConfiguration.java中,我使用下面這些參數來連接mySQL數據庫。但這是行不通的。我的應用程序和mysql服務器是OpenShift上的同一個應用程序。如何將我的Spring應用程序連接到OpenShift上的mySQL服務器?

private final String DATABASE_DRIVER = "com.mysql.jdbc.Driver"; 

private final String DATABASE_URL = "jdbc:mysql://" 
      + System.getenv("OPENSHIFT_MYSQL_DB_HOST") + ":" 
      + System.getenv("OPENSHIFT_MYSQL_DB_PORT") + "/" 
      + "capsule";//System.getenv("OPENSHIFT_APP_NAME"); 

private final String DATABASE_USERNAME = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"); 
private final String DATABASE_PASSWORD = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"); 

請給我一些想法。非常感謝。

+2

什麼是「不行」是什麼樣子?錯誤訊息?堆棧痕跡?行爲? – duffymo

回答

1

春天JPA的應用程序,你必須定義數據源之後

@Bean(destroyMethod = "close") 
public DataSource dataSource() { 
    String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"); 
    String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"); 
    String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST"); 
    String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT"); 
    String databaseName = System.getenv("OPENSHIFT_APP_NAME"); 
    String url = "jdbc:mysql://" + host + ":" + port + "/"+databaseName; 
    BasicDataSource dataSource = new BasicDataSource(); 
    dataSource.setDriverClassName("org.MYSQL.Driver"); 
    dataSource.setUrl(url); 
    dataSource.setUsername(username); 
    dataSource.setPassword(password); 
    dataSource.setTestOnBorrow(true); 
    dataSource.setTestOnReturn(true); 
    dataSource.setTestWhileIdle(true); 
    dataSource.setTimeBetweenEvictionRunsMillis(1800000); 
    dataSource.setNumTestsPerEvictionRun(3); 
    dataSource.setMinEvictableIdleTimeMillis(1800000); 
    dataSource.setValidationQuery("SELECT version()"); 

    return dataSource; 
} 

如果你想看到一個完整的工作示例然後結帳我的github倉庫https://github.com/shekhargulati/forumapp

+0

感謝您的關注。我最近修好了。 – TruongPS

相關問題