下面是关于JDBC连接配置中最重要的配置:
1. hibernate.connection.driver_class : 设置连接数据库的驱动.
2. hibernate.connection.url : 设置所需连接数据库服务的URL
3. hibernate.connection.username : 连接数据库的用户名
4. hibernate.connection.password : 连接数据库的密码
5. hibernate.connection.pool_size : 设置hibernate数据库连接池的最大并发连接数
6. hibernate.dialect : 设置连接数据库使用的方言
上面配置了Hibernate数据库连接池的最大连接并发数,但是Hibernate自带的连接池仅有测试价值。并不推荐在实际的项目中使用。实际项目中可以使用c3p0连接池。所以使用c3p0连接池配置代替hibernate.connection.poo_size即可。
c3p0配置如下:
1. hibernate.c3p0.max_size : c3p0连接池的最大连接数
2. hibernate.c3p0.min_size : c3p0连接池的最小连接数
3. hibernate.c3p0.timeout : c3p0连接池连接的超时时长,单位为秒
4. hibernate.c3p0.max_statements : c3p0缓存 Statement的数量数
故,完整的配置文件内容如下(使用MySQL数据库):
Xml代码 复制代码
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 设置数据库连接的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 设置所需连接数据库服务的URL localhost 可以 改成远程IP地址 onlinestore 为数据库名 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost/onlinestore</property>
<!-- 设置连接数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 设置连接数据库的密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 设置显示sql语句 方便调试-->
<property name="hibernate.show_sql">true</property>
<!-- c3p0连接池的最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- c3p0连接池的最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- c3p0连接池连接的超时时长 如果空闲连接的空闲超过了timeout,就会删除 -->
<property name="hibernate.c3p0.timeout">1800</property>
<!-- c3p0缓存 Statement的数量数 -->
<property name="hibernate.c3p0.max_statements">50</property>
<!-- sql连接方言此处为MySQL -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
Hibernate配置文件hibernate.cfg.xml的数据库连接配置
最新推荐文章于 2020-07-31 16:33:53 发布