2011-12-01 103 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" />