2012-02-03 11 views
9

Sto provando a stabilire una seconda connessione di database a un altro database su un altro server. Stiamo usando il framework 1.2.4 e ho trovato la seguente documentazione per 1.2.3.Database multipli nel framework di gioco

http://www.playframework.org/documentation/1.2.3/model#multiple

application.conf: 
db_other.url=jdbc:mysql://localhost/test 
db_other.driver=com.mysql.jdbc.Driver 
db_other.user=root 
db_other.pass= 

Connection conn = DB.getDBConfig("other").getConnection() 

Questo non ha funzionato per me così ho fatto un po 'più di ricerca e ha trovato il seguente articolo. In questo articolo mi ha detto che la configurazione di cui sopra trapelato in dal ramo 1.3 padrone e sarà disponibile in futuro ...

JPA.getJPAConfig method not found on Play's API

https://play.lighthouseapp.com/projects/57987/tickets/706

Qualcuno può darmi un modo per fare qualche semplice query a quell'altro database? Penso di non essere l'unico a voler utilizzare più database.

Grazie!

+0

Per le query a un altro db sullo stesso im server utilizzando questo test List = JPA.em(). createNativeQuery ("SELECT * FROM other_db..TABLE"). getResultList(); in sybase – n4cer

risposta

7

Per leggere di tanto in tanto i dati da altri database, è possibile utilizzare anche Plain Old JDBC:

Connection c = null; 
    PreparedStatement pstmt = null; 
    ResultSet rs = null; 

    try { 
     String url = "YourJdbcUrl"; 
     Class.forName("YourDriver").newInstance(); 
     c = DriverManager.getConnection(url, "XXX", "XXX"); 
     pstmt = c.prepareStatement("SELECT * FROM TABLE"); 
     rs = pstmt.executeQuery(); 
     while (rs.next()) { 
      // Fill your data into Play Model instances here. 
     } 

    }catch(Exception e){ 
     e.printStackTrace(); 
    } finally { 
     try { if (rs != null) rs.close(); } catch (Exception e) {}; 
     try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}; 
     try { if (c != null) c.close(); } catch (Exception e) {}; 
    } 

    render(...); 
+1

Funziona come un incantesimo, grazie! – dreampowder

1

Ecco come mi collego ad altri database ora finché non c'è un'altra soluzione.

Connection c = null; 
try { 
    ComboPooledDataSource ds = new ComboPooledDataSource(); 
    ds.setDriverClass("com.sybase.jdbc3.jdbc.SybDriver"); 
    ds.setJdbcUrl("jdbc:sybase:Tds:server:4100/db"); 
    ds.setUser("user"); 
    ds.setPassword("pass"); 
    ds.setAcquireRetryAttempts(10); 
    ds.setCheckoutTimeout(5000); 
    ds.setBreakAfterAcquireFailure(false); 
    ds.setMaxPoolSize(30); 
    ds.setMinPoolSize(1); 
    ds.setMaxIdleTimeExcessConnections(0); 
    ds.setIdleConnectionTestPeriod(10); 
    ds.setTestConnectionOnCheckin(true); 

    DB.datasource = ds; 
    try { 
     c = ds.getConnection(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    } catch (PropertyVetoException e) { 
     e.printStackTrace(); 
} 

String sql = "SELECT * FROM TABLE"; 

try { 
    PreparedStatement pstmt = c.prepareStatement(sql); 
    ResultSet rs = pstmt.executeQuery(); 

    while (rs.next()) { 
     System.out.println(rs.getString(1)+"\n"); 
    } 

    c.close(); 
} catch (SQLException e) { 
    e.printStackTrace(); 
} 
Problemi correlati