2

How to access database and run custom executable or query on it? after some search i found that i can use sessionFactory, but i have to force hibernate as data handler, well that make me worry if it have some bad effect or behavior in my complex part of application, depending on the way it links data together, or the way it fetches joined data and handles them... so i want to go with spring default... also if it doesn't preferred hibernate, it may mean that hibernate is slower. i want to access the online session (previously i used NHibernate on C#, and creating secondary session could be problematic), so i want to access exists session of database, beside when i use JpaRepository, and run the custom command on it. so i don't disturb other session, or locks :|

BTW i'm using HSQLDB for now...

Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55

1 Answers1

2

Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath). In addition Spring Boot will load the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform, e.g. you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql etc.).The script locations can be changed by setting spring.datasource.schema and spring.datasource.data, and neither location will be processed if spring.datasource.initialize=false.

More here

If you put the hibernate jars in you classpath then Spring's autoconfiguration will take over and think that you wanna user Hibernate. So make sure you don't have those jars in your classpath.

Ulises
  • 9,115
  • 2
  • 30
  • 27
  • what about running the custom script? and i want to run it at run time, not on startup... what component should get wired and what script should get runned...? – Hassan Faghihi Dec 24 '16 at 06:04
  • You'll have to use the data source directly, read the script file and run the queries. Shouldn't be too hard. Or you'd have to debug spring to see if you can leverage any existing code. But you'd probably can run the script when you want by just reading the statements from the file, and in pretty sure there are ton of examples out the on how to run a script against a datasource – Ulises Dec 24 '16 at 06:14
  • data source directly, you mean, connection, and createStatement way? well i hardly found two of them yesterday, and both generate error, BTW, If i use datasource directly and you meant what i thought, wouldn't it cause any trouble when the spring framework session is running/is online? – Hassan Faghihi Dec 24 '16 at 07:04
  • https://www.mkyong.com/jdbc/how-to-run-a-mysql-script-using-java/ that's one using a ScriptRunner from ibatis. And no, it shouldn't cause any issues. – Ulises Dec 24 '16 at 07:07
  • another one https://github.com/BenoitDuffez/ScriptRunner. I see a bunch of them out there... – Ulises Dec 24 '16 at 07:09
  • Thank you, i look into this in details once i got home, to see if it let other operation beside DML. i had never heard of scriptRunner before... – Hassan Faghihi Dec 24 '16 at 07:09