Unable to get BLOBs onto the file system

57 views
Skip to first unread message

Pablo

unread,
Oct 28, 2011, 3:15:05 PM10/28/11
to H2 Database
A follow up on that last submission; I've now written a test app and
seem unable to cause H2 to put my BLOB object in an external file on
the file system. Since I'm new to H2 and a novice at DBs, I'm thinking
I'm missing something; but if so, I'm not finding the documentation
that is telling what to do differently.

I have 5 files involved:
- persistence.xml
- create_db.sql
- Table1.java
- DBTest2.java
- data.txt - a datafile with about 1MB of text

After running create-db.sql in the H2 Console, I then run DBTest2. The
results are a TestDB.h2.db file that is 2MB and no TestDB.lobs.db
directory. I expected a single lob file to have been created.

The files look like:
- persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">
<persistence-unit name="TestDB" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>dbtest.Table1</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:TestDB/
TestDB;LOCK_MODE=0"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="awi1165"/>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.H2Platform"/>
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>

- create_db.sql
SET MAX_LENGTH_INPLACE_LOB 200;
DROP TABLE IF EXISTS TABLE_1;
CREATE TABLE TABLE_1 ( INDEX INT, BLOB_1 BLOB );

- Table1.java
package dbtest;
import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table( name = "TABLE_1" )
public class Table1 implements Serializable
{
@Id
@Column( name = "INDEX" )
private int index;

@Lob( )
@Basic( fetch = javax.persistence.FetchType.LAZY )
@Column( name = "BLOB_1" )
private byte[] blob;

public Table1()
{
}

public Table1( int value )
{
this.index = value;
}

public void setBlob1( byte[] blobValue )
{
this.blob = blobValue;
}
}

- DBTest2.java
package dbtest;

import java.io.*;
import javax.persistence.*;

public class DBTest2
{
private static EntityManagerFactory factory;

public static void main( String[] args )
{
factory = Persistence.createEntityManagerFactory( "TestDB" );
EntityManager em = factory.createEntityManager();

em.getTransaction().begin();
Table1 t1 = new Table1( 2 );
t1.setBlob1( getBlob() );
em.persist( t1 );
em.getTransaction().commit();

em.close();
}

private static byte[] getBlob()
{
byte[] bytes = new byte[0];
try
{
File dataFile = new File( "data.txt" );
if ( dataFile.isFile() )
{
DataInputStream dis = new DataInputStream( new
FileInputStream( dataFile ) );
bytes = new byte[(int)dataFile.length()];
dis.read( bytes );
dis.close();
}
}
catch ( IOException x )
{
System.err.println("IOException: " + x.getMessage() );
System.exit( 1 );
}
return bytes;
}
}

Thomas Mueller

unread,
Nov 1, 2011, 4:22:00 PM11/1/11
to h2-da...@googlegroups.com
Hi,

> The
> results are a TestDB.h2.db file that is 2MB and no TestDB.lobs.db
> directory.

Yes, newer versions of H2 (1.3.x) store blobs in the database. See the
change log at http://www.h2database.com/html/changelog.html

> I expected a single lob file to have been created.

That's no longer the case.

Regards,
Thomas

Pablo

unread,
Nov 2, 2011, 6:28:50 PM11/2/11
to H2 Database
Okay, the change log eventually exposed the h2.lobInDatabase property.
Setting that to false did what I was asking for.
Reply all
Reply to author
Forward
0 new messages