2011-12-01 102 views
3

是否有可能使JPA使用SSL進行連接。我不太瞭解JPA,因此我無法提供有關使用哪個版本和提供程序的許多詳細信息,但這裏是我的persistence.xml文件的一個簡化版本。是否有可以添加的屬性使其使用安全連接?SSL與EclipseLink JPA

<?xml version="1.0" encoding="UTF-8" ?> 
<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" xmlns="http://java.sun.com/xml/ns/persistence"> 
<persistence-unit name="MY_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL"> 
    <!-- declare class entities here --> 

    <properties> 
     <property name="javax.persistence.jdbc.url" value="jdbc:mysql://URL_TO_SERVER" /> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
     <property name="javax.persistence.jdbc.user" value="USERNAME" /> 
     <property name="javax.persistence.jdbc.password" value="PASSWORD" /> 

     <!-- Optimize database writes to use batching. --> 
     <property name="eclipselink.jdbc.batch-writing" value="JDBC" /> 

     <!-- Avoids flush being triggered before every query execution. --> 
     <property name="eclipselink.persistence-context.flush-mode" 
      value="COMMIT" /> 

     <!-- Configure connection pool. --> 
     <property name="eclipselink.jdbc.connections.initial" value="1" /> 
     <property name="eclipselink.jdbc.connections.min" value="64" /> 
     <property name="eclipselink.jdbc.connections.max" value="64" /> 

     <!-- Timeout for connection. --> 
     <property name="eclipselink.jdbc.timeout" value="10" /> 

     <!-- Configure cache size. --> 
     <property name="eclipselink.cache.size.default" value="1000" /> 

     <!-- Configure database to be created on startup if not already existing.--> 
     <property name="eclipselink.ddl-generation" value="create-tables" /> 

     <!-- Configure simple SQL logging for demonstration. --> 
     <property name="eclipselink.logging.level" value="FINE" /> 
     <property name="eclipselink.logging.thread" value="false" /> 
     <property name="eclipselink.logging.session" value="false" /> 
     <property name="eclipselink.logging.exceptions" value="false" /> 
     <property name="eclipselink.logging.timestamp" value="false" /> 
    </properties> 
</persistence-unit> 

回答

4

這是沒有什麼具體的JPA,只是添加參數JDBC連接字符串。假定其他一切都設置正確,然後加入這已經足夠了:

?useSSL=true&requireSSL=true 

如果在一般的SSL連接不工作,那麼這個頁面提供了更多信息:MySQL 20.3.4.5. Connecting Securely Using SSL

+0

謝謝!這些信息看起來很有希望得到這個工作。在初始化數據庫連接(使用System.setProperty)之前,我是否必須在java代碼中設置這些屬性,或者可以在問題中設置persistence.xml文件中的屬性(使用 Nitrex88

+0

請注意,我不直接使用JDBC連接字符串(JPA處理此問題,這就是爲什麼JPA特定於我) – Nitrex88

+0

不會有太大的差別,參數可以添加到javax.persistence.jdbc的值中。網址 –

0

我發現這是有用的EclipseLink 2.5.0通過JDBC驅動程序傳遞性能:

<property name="eclipselink.jdbc.property.my_property_name" value="my_value" /> 

這是特定的驅動程序,但在你的情況下,這將是:

<property name="eclipselink.jdbc.property.useSSL" value="true" /> 
<property name="eclipselink.jdbc.property.requireSSL" value="true" />