You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询基于Java Hibernate与Angular的Web服务中H2数据库的大小?

How to Check Your H2 Database Size

Got it, let's break down the practical ways you can check the size of your file-based H2 database—since you mentioned finding H2 files, these methods will fit your setup perfectly:

1. Use H2's Built-in SQL Queries

This is the most straightforward approach if you can run SQL against your database (via H2 console, your app's query layer, or a database tool):

  • To get the size of the main persistent data file (in bytes):
    SELECT FILE_SIZE() FROM INFORMATION_SCHEMA.SYSTEM_USERS;
    
    Convert the result to KB/MB by dividing by 1024 or 1024*1024 respectively.
  • For a total of all associated files (data file, transaction logs, etc.), use:
    SELECT SUM(FILE_LENGTH) AS TOTAL_DB_SIZE_BYTES FROM INFORMATION_SCHEMA.FILES;
    
    This sums up the size of every file linked to your H2 database instance.

2. Check the File System Directly

H2 file-based databases create several files in the directory specified in your Hibernate JDBC URL (look for the path starting with jdbc:h2:file:/). Key files include:

  • your-db-name.mv.db: The main data file (largest one)
  • your-db-name.trace.db: Transaction/log file
  • your-db-name.lock.db: Small lock file (negligible size)
    Calculate their total size directly on your system:
  • On Windows: Select the files, right-click → "Properties" to see total size
  • On macOS/Linux: Run du -sh /path/to/your/db-directory/ in the terminal to get the directory's total size

3. Fetch Size Programmatically via Hibernate/JPA

If you need to get the database size directly in your Java code, execute the SQL queries above using Hibernate Session or JPA EntityManager:

Using Hibernate Session:

try (Session session = sessionFactory.openSession()) {
    Long fileSizeBytes = (Long) session.createNativeQuery("SELECT FILE_SIZE()").uniqueResult();
    double fileSizeMB = fileSizeBytes / (1024.0 * 1024.0);
    System.out.println("Database size: " + fileSizeMB + " MB");
}

Using JPA EntityManager:

try (EntityManager em = entityManagerFactory.createEntityManager()) {
    Long totalSizeBytes = (Long) em.createNativeQuery("SELECT SUM(FILE_LENGTH) FROM INFORMATION_SCHEMA.FILES")
                                   .getSingleResult();
    double totalSizeMB = totalSizeBytes / (1024.0 * 1024.0);
    System.out.println("Total database size (all files): " + totalSizeMB + " MB");
}

4. Use the H2 Console

If you have the H2 console enabled (common in dev setups):

  1. Launch it (usually at http://localhost:8082 if running with your app)
  2. Connect using your database's JDBC URL, username, and password
  3. Run one of the SQL queries from method 1—you'll see the size result right away.

Quick note: If you were using an in-memory H2 instance (jdbc:h2:mem:), there are no persistent files, so FILE_SIZE() would return 0. But since you found H2 files, this doesn't apply to your case.

内容的提问来源于stack exchange,提问作者user12465043

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:44:06